Data in JSON format is not shown

Asked

Viewed 298 times

0

Hello, I have the following php code:

<?php

require("config_local.php"); //conexao com o banco de dados

$area = "eua";
$st = "ny";


$sql = $pdo->prepare("SELECT prop,old_no,emissao,area,country,e_name FROM table1 WHERE area = :area AND st= :st ");
$sql->bindValue(":area",$area,PDO::PARAM_STR);
$sql->bindValue(":st",$st,PDO::PARAM_STR);
$sql->execute();

$result = $sql->fetchAll();
$n = $sql->rowCount();


echo $n ; // mostra o numero correto
echo json_encode($result); // nao mostra nada, tela em branco

The problem is that when I try to execute this query, it returns me the blank screen, but if I take one of the fields to be shown, I receive the data correctly.

The question is, is there some kind of PHP limitation on showing large amounts of data/fields in JSON format? If yes, how could I dribble it? Because I need to show these and some other fields...

Thank you.

  • There seems to be nothing wrong with your code, try validating the return of the "json_encode" function if instead of a string, return the false Boolean, call json_last_error to know what the problem is. function documentation: http://php.net/manual/en/function.json-error.php

  • No, the code works normally, the problem is that php seems to "limit" the amount of information to be shown. I’ve been testing the query by adding a field at a time, which comes to a point that when you add 1 more field, it stops displaying.

  • I used jsonlasterror, I took the exception JSON_ERROR_UTF8, if you get something put here...

1 answer

2


Opa, was coding error utf8.

I decided to put in my class of connection to line:

$pdo->query("SET NAMES utf8;");

Thus remaining:

<?php
try{
 $pdo = new PDO("mysql:host=localhost;dbname=mydb","root","");
 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $pdo->query("SET NAMES utf8;");
 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

}catch(PDOException $e){

 echo 'Falha ao conectar - ERROR:' . $e->getMessage();
}

Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.