Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

gabrielfalieri

retorno de json_encode inválido

Recommended Posts

Não faz o menor sentido, por exemplo, eu tenho uma outra classe que também faz a mesma coisa, só que sem filtros.

<?php
//tirei meus dados de conexão
$servername = "";
$username = "";
$password = "";
$dbname = "";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

/* Database connection end */


// storing  request (ie, get/post) global array to a variable
$requestData= $_REQUEST;


$columns = array(
// datatable column index  => database column name
    0 =>'razao_social',
    1 => 'cnpj',
    2 => 'insc_estadual',
    3 => 'insc_municipal',
    4 => 'end_estado',
    5 => 'id_partner'

);

// getting total number records without any search
$sql = "SELECT razao_social,cnpj,insc_estadual,insc_municipal,end_estado, id_partner ";
$sql.=" FROM prt_partner WHERE _ativo = '1'";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData;  // when there is no search parameter then total number rows = total number filtered rows.


$sql = "SELECT razao_social,cnpj,insc_estadual, insc_municipal, end_estado, id_partner ";
$sql.=" FROM prt_partner WHERE _ativo = '1' ";
if(!empty($requestData['search']['value'])){
    $sql.=" AND razao_social LIKE '%".$requestData['search']['value']."%' AND _ativo = '1'".
        "OR cnpj LIKE '%".$requestData['search']['value']."%' AND _ativo = '1'".
        "OR insc_estadual LIKE '%".$requestData['search']['value']."%' AND _ativo = '1'".
        "OR insc_municipal LIKE '%".$requestData['search']['value']."%' AND _ativo = '1'".
        "OR end_estado LIKE '%".$requestData['search']['value']."%' AND _ativo = '1'";
}

$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]."   ".$requestData['order'][0]['dir']."  LIMIT ".$requestData['start']." ,".$requestData['length']."   ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc  */
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");

$data = array();
while( $row=mysqli_fetch_array($query) ) {  // preparing an array
    $nestedData=array();

    $nestedData[] = $row["razao_social"];
    $nestedData[] = $row["cnpj"];
    $nestedData[] = $row["insc_estadual"];
    $nestedData[] = $row["insc_municipal"];
    $nestedData[] = $row["end_estado"];

    $nestedData[] =
        "<a href='reg-partner.php?id=".$row['id_partner']."&acao=editar'><button class='btn btn-warning'><i class='fa fa-edit'></i> </button></a>".
        "   ".
        "<a href='reg-partner.php?id=".$row['id_partner']."&acao=excluir'><button class='btn btn-danger'><i class='fa fa-close'></i> </button></a>".
        "   "
        ."<button class='btn btn-success'><a href='detail-partners.php?id=".$row['id_partner']."'><i class='fa fa-list'></i> </a></button>";
    $data[] = $nestedData;
}



$json_data = array(
    "draw"            => intval( $requestData['draw'] ),   // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
    "recordsTotal"    => intval( $totalData ),  // total number of records
    "recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
    "data"            => $data   // total data array
);

echo json_encode($json_data);  // send data as json format
?>

Coloco esse código na página e funciona normalmente, assim que eu mudo para os parametros da tabela que preciso, da o erro

<?php

$servername = "";
$username = "";
$password = "";
$dbname = "";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

/* Database connection end */


// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;


$columns = array(
// datatable column index => database column name
0 =>'razao_social',
1 => 'nome',
2 => 'cargo',
3 => 'tel_comercial1',
4 => 'tel_celular1',
5 => 'email1',
6 => 'id_resp'

);

// getting total number records without any search
$sql = "SELECT razao_social, nome, cargo, tel_comercial1, tel_celular1, email1, id_resp ";
$sql.=" FROM responsibles_view ";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.


$sql = "SELECT razao_social, nome, cargo, tel_comercial1, tel_celular1, email1, id_resp ";
$sql.=" FROM responsibles_view WHERE 1 = 1";
if(!empty($requestData['search']['value'])){
$sql.=" AND razao_social LIKE '%".$requestData['search']['value']."%'".
"OR nome LIKE '%".$requestData['search']['value']."%'".
"OR cargo LIKE '%".$requestData['search']['value']."%'".
"OR tel_comercial1 LIKE '%".$requestData['search']['value']."%'".
"OR cel_comercial1 LIKE '%".$requestData['search']['value']."%'".
"OR email1 LIKE '%".$requestData['search']['value']."%'".
"OR id_resp LIKE '%".$requestData['search']['value']."%'";
}

$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result.
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
/* $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc */
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");

$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();

$nestedData[] = $row["razao_social"];
$nestedData[] = $row["nome"];
$nestedData[] = $row["cargo"];
$nestedData[] = $row["tel_comercial1"];
$nestedData[] = $row["tel_celular1"];
$nestedData[] = $row["email1"];

$nestedData[] =
"<a href='reg-partner.php?id=".$row['id_resp']."&acao=editar'><button class='btn btn-warning'><i class='fa fa-edit'></i> </button></a>".
"   ".
"<a href='reg-partner.php?id=".$row['id_resp']."&acao=excluir'><button class='btn btn-danger'><i class='fa fa-close'></i> </button></a>".
"   "
."<button class='btn btn-success'><a href='detail-partners.php?id=".$row['id_resp']."'><i class='fa fa-list'></i> </a></button>";
$data[] = $nestedData;
}



$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);

echo json_encode($json_data); // send data as json format
?>

Ai da o erro.
Estou puxando os dados de uma view (que funciona perfeitamente)

CREATE 
    ALGORITHM = UNDEFINED 
    DEFINER = `eco_portaluser`@`%` 
    SQL SECURITY DEFINER
VIEW `responsibles_view` AS
    SELECT 
        `prt_par`.`razao_social` AS `razao_social`,
        `prt_resp`.`nome` AS `nome`,
        `prt_resp`.`cargo` AS `cargo`,
        `prt_resp`.`tel_comercial1` AS `tel_comercial1`,
        `prt_resp`.`tel_celular1` AS `tel_celular1`,
        `prt_resp`.`email1` AS `email1`,
        `prt_resp`.`id_resp` AS `id_resp`
    FROM
        (`prt_partner` `prt_par`
        JOIN `prt_partner_responsible` `prt_resp`)
    WHERE
        (`prt_resp`.`_id_partner` = `prt_par`.`id_partner`)
    ORDER BY `prt_par`.`id_partner`

Compartilhar este post


Link para o post
Compartilhar em outros sites

Jo�o

Até onde sei Json é nativamente utf-8.

Toda a string deve ser codificada como UTF-8.

http://php.net/manual/pt_BR/function.json-encode.php

Isto pode ser um erro, (fora q é moh 'amador') entregar com erro de charset conding

Corrigi isso tmb.

pesquisa por

utf8_encode()

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.