Return with different values in JSON / PHP

Asked

Viewed 783 times

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;

  • I’ll try and see if it works!

  • I made the suggested changes @claudsan, but only print_r($line); works. But nothing with json_encode.

  • I edited the question code, ran a new test on the code.

  • I will try to set an example like yours here to try to understand better. And I come back here again.

  • Blz!! In the database, the column has the following characteristics: type: TEXT, Grouping (Collation): utf8_bin. I am using XAMP 1.8.3-5

  • 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.

  • you just made the addition of $Count to control the looping? That’s all?

  • yes, I saw nothing strange in the function, your data is just text? has some special character? or html inside?

  • Friend, I copied and pasted your code here, Count returned to me 817 records, but the screen is blank :/

  • The data is only text yes, only that there are many :/

Show 7 more comments

1 answer

2


Solution:

 <?php

include "conexao.php";   

mysql_set_charset('utf8'); //<--- SOLUÇÃO

// 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();
$count = 0;
while($r = mysql_fetch_object($resultado))
{  
    $linha [$count] = $r->todo_resto; 
    $count++;

}
// FUNCIONA
echo "total: $count<hr><pre>\n\n";
echo json_encode($linha);


mysql_close(); 


?>

Reference: http://php.net/manual/en/function.mysql-set-charset.php

  • 1

    Great solution, clean fast and efficient!! @claudsan Thank you very much!!

Browser other questions tagged

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