0
Scenario: I have a query made in PHP and need to return it to my iOS app in JSON format. But when doing some tests, in the browser, I noticed that when using the function echo json_enconde($resultado);
returns fewer results on the screen. For example, my database has about 200 records in one column, but using the json_encode
, only a few column values appear, and when I use only echo
, returns all results.
Does anyone know why there is such a reduction in values using json_encode
?
Below is my PHP code:
include "conexao.php";
// Pucha o conteudo do processo que fica na coluna todo_resto
$sql = "select todo_resto from registros";
$resultado = mysql_query($sql) or die ("Erro .:" . mysql_error());
// Criando variável linha do tipo array
$linha = array();
while($r = mysql_fetch_object($resultado))
{
$linha [] = $r->todo_resto;
}
// Não funciona
// echo json_encode($linha);
// Não funciona
// print_r(json_encode($linha));
mysql_close();
?>
After a few tests I made a change in the while
of the code thus remaining:
while($r = mysql_fetch_object($resultado))
{
echo ($r->todo_resto)."######";
}
able to print all values, but when I enter the json_encode
, only returns some values of the database, the rest are empty (null).
while($r = mysql_fetch_object($resultado))
{
echo json_encode($r->todo_resto)."#";
}
Tries to replace the
$r = mysql_fetch_object($resultado)
for$r = mysql_fetch_array($resultado)
. And$linha [] = $r->todo_resto;
by only$linha[] = $r;
– claudsan
I’ll try and see if it works!
– Tiago Amaral
I made the suggested changes @claudsan, but only print_r($line); works. But nothing with json_encode.
– Tiago Amaral
I edited the question code, ran a new test on the code.
– Tiago Amaral
I will try to set an example like yours here to try to understand better. And I come back here again.
– claudsan
Blz!! In the database, the column has the following characteristics: type: TEXT, Grouping (Collation): utf8_bin. I am using XAMP 1.8.3-5
– Tiago Amaral
give a look here it worked: http://arminio.com.br/teste.php and here http://arminio.com.br/teste.php?fonte the only thing I changed was the $Count variable.
– claudsan
you just made the addition of $Count to control the looping? That’s all?
– Tiago Amaral
yes, I saw nothing strange in the function, your data is just text? has some special character? or html inside?
– claudsan
Friend, I copied and pasted your code here, Count returned to me 817 records, but the screen is blank :/
– Tiago Amaral
The data is only text yes, only that there are many :/
– Tiago Amaral
Let’s go continue this discussão in chat.
– claudsan