This question already has an answer here:

I have some data stored in a MySQL table and I get that out with PDO. The function puts the result of the query into an array and all the fields are present:

  Array ( 
    [0] => Array ( [id] => 1 [nome] => Oggetto 1 [descr] => Questa è la favolosa descrizione dell'oggetto 1 [prezzo] => 50.00 [imgpath] => dummy.jpg [url] => http://localhost/ [cat] => 1 [avail] => 1 ) 
    [1] => Array ( [id] => 2 [nome] => Oggetto 2 [descr] => Questa è la favolosa descrizione dell'oggetto 2 [prezzo] => 45.00 [imgpath] => dummy.jpg [url] => http://localhost/ [cat] => 1 [avail] => 1 ) 
    [2] => Array ( [id] => 3 [nome] => Oggetto 3 [descr] => Questa è la meravigliosa descrizione dell'oggetto 3 [prezzo] => 120.00 [imgpath] => dummy.jpg [url] => http://localhost/ [cat] => 3 [avail] => 1 ) 
    [3] => Array ( [id] => 4 [nome] => Oggetto 4 [descr] => Questa è la meravigliosa descrizione dell'oggetto 4 [prezzo] => 200.00 [imgpath] => dummy.jpg [url] => http://localhost [cat] => 2 [avail] => 1 ) 
    [4] => Array ( [id] => 5 [nome] => Oggetto 5 [descr] => Questa è la fantasiosa descrizione dell'oggetto 5 [prezzo] => [imgpath] => dummy.jpg [url] => http://locahost [cat] => 4 [avail] => 1 ) )

The thing is I need a json formatted data response so I use the json_encode function to turn the PHP array into JSON formatted data, but as you can see from the following output the field descr has gone missing and I can't figure out why:

    [
      {"id":"1","nome":"Oggetto 1","descr":null,"prezzo":"50.00","imgpath":"dummy.jpg","url":"http:\/\/localhost\/","cat":"1","avail":"1"},
      {"id":"2","nome":"Oggetto 2","descr":null,"prezzo":"45.00","imgpath":"dummy.jpg","url":"http:\/\/localhost\/","cat":"1","avail":"1"},
      {"id":"3","nome":"Oggetto 3","descr":null,"prezzo":"120.00","imgpath":"dummy.jpg","url":"http:\/\/localhost\/","cat":"3","avail":"1"},
      {"id":"4","nome":"Oggetto 4","descr":null,"prezzo":"200.00","imgpath":"dummy.jpg","url":"http:\/\/localhost","cat":"2","avail":"1"},
      {"id":"5","nome":"Oggetto 5","descr":null,"prezzo":null,"imgpath":"dummy.jpg","url":"http:\/\/locahost","cat":"4","avail":"1"}
    ]

can this be due to the fact I have declared the desc as text field in the table? If so how do I make sure the data in that field doesn't get lost while passing from PHP array to JSON formatted data?

share|improve this question
Please pretty-print your output, it's hard to read on just one line. – str 2 days ago
@str hopefully it's more readable now! – haunted85 2 days ago
json_encode expects an unicode string, but a string in a different encoding may be an invalid unicode string because of the accented characters. – biziclop 2 days ago

marked as duplicate by mario, Deanna, g.d.d.c, Lukas Knuth, rcdmk 2 days ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

up vote 0 down vote accepted

Check your descr field values, the character è is not executable character in JSON, so use htmlentities($_POST[descr]) before getting array results.

Ex:

<?php
    $a = htmlentities("Questa è la favolosa descrizione dell'oggetto 1");
    $arr = array('a' => $a);

    echo json_encode($arr);
?>

Result is,

{"a":"Questa è la favolosa descrizione dell'oggetto 1"}

http://codepad.org/M2P8mnR8

NOTE: you can use UTF8 to encode those chracters using MYSQL itself

share|improve this answer

Because the [descr] string contains extended characters, you may need to encode it with something like PHP's htmlentities function.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.