简体   繁体   中英

json_encode for mysql query returns some null columns of some rows, but the columns aren't null

I'm creating a little php file to get mysql rows for my android application. to retrieve each single row i use:

    $q=mysql_query($sql, $this->conn) or die (mysql_error());
while($e=mysql_fetch_assoc($q)) {
      $output[]=$e;
    }
print(json_encode($output));

but sometimes it returns me an inconsistent output.

Here's an example: The row:

id=1 (correct) - name="name1" (correct) - price=200 (correct) - price2=null (correct) - area=null (NOT CORRECT) because area has a value of "Centro Città" .. and so on..

How can i resolve this problem?

EDIT: the value is

[{"id":"84","idutente1":null,"idutente2":null,"idutente3":null,"idutente4":null,"idutente5":null,"idagente":null,"annuncio":"0","archiviato":"0","dataarchiviazione":"2012-07-05 13:31:19","tipoimmobile":"Residenziale","metodo":"Vendita","area":null,"presentazione":"1","homepage":"1","mappa":"1","nome":"Piazza Umberto I","luogo":"Crotone","descrizione":"60 mq composto da due vani oltre servizi, centralissimo buono per ufficio.","descdettagliata":" L'appartamento è diviso in due vani con cucina e bagno.</p>\\r\\nPosto nel pieno centro e vicinissimo al lungomare Regina Margherita.</p>\\r\\n Ottimo condominio. Libero e disponibile da subito. /p>","vani":"2","prezzo":"87000","prezzo1":"0","prezzo2":"0","prezzo3":"0","prezzo4":"0","prezzo5":"0","metriquadri":"58","stato":"In buono stato","riscaldamento":"Autonomo","classeenergetica":"Non dotato","piano":"Primo/secondo/terzo","indicazioni":"","mutuo":"No","banca":"","importo":"","finalita":"","tipologia":"","ammortamento":"","visite":"20","timest amp":"2012-06-25 10:48:01","nomefile":"HPIM2268.jpg"}]

area field is not null, but is "Centro Città". The rest is correct. I tried to show only the "area" field with a mysql_fetch_assoc($query) and it returns me the correct result: "Centro Città". This problem is only with json_encode.

The SQL Variabile is:

$sql = sprintf("SELECT i.*, f.nomefile FROM immobili i LEFT JOIN foto f on(i.id=f.idimmobile AND f.copertina=1) WHERE i.id=%s", $_GET['id']);

This is only an example, if i ask other rows, the area field returns correctly also with json_encode and other fields return null. Could it be a problem related with the strings and the text format?

Problem solved

$result = $mysql->query($sql);
while($row = mysql_fetch_assoc($result)) {
   $r[] = array_map('utf8_encode', $row);
}
echo json_encode($r);

Thank you

It's an encoding problem. json_encode() accepts only UTF-8 strings, and returns null if a string is not valid UTF-8.

You can fix this by converting the encoding of your data before passing it to json_encode. Take a look at iconv() or mb_convert_encoding() .

For example, assuming your data is encoded in latin1:

$e = array_map(function($string) {
    return iconv("iso-8859-1", "utf-8", $string);
}, $e);

$output[] = $e;

Or if you have php < 5.3:

function cb($string) {
    return iconv("iso-8859-1", "utf-8", $string);
}

$e = array_map('cb', $e);

$output[] = $e;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM