0

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?

3
  • Please pretty-print your output, it's hard to read on just one line. Commented Feb 20, 2013 at 11:00
  • @str hopefully it's more readable now! Commented Feb 20, 2013 at 11:05
  • json_encode expects an unicode string, but a string in a different encoding may be an invalid unicode string because of the accented characters. Commented Feb 20, 2013 at 11:31

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.