Reputation: 4557
I am working on facebook app
. I have stored some values in database in json format.
When I am fetching the value on the target page and trying to display the value as follow :
$sql = mysql_query("select * from `beforepublish` where `tabid` = '$page_id'") or die(mysql_error());
$sql = mysql_fetch_assoc($sql);
$contest_id = $sql['contestid'];
$temp_id = $sql["tempid"];
$value = $sql["values"];
$returnValue = json_decode($value);
echo "<pre>";
print_r($returnValue);
echo "</pre>";
Here the value of
$value = {"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab "}
and I am getting $returnValue
as blank.
but When I try :
$returnValue = json_decode('{"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab "}');
echo "<pre>";
print_r($returnValue);
echo "</pre>";
then $returnValue is returning :
stdClass Object
(
[image] => upload_1182341221.jpg
[conttxt] => Get a chance to win Samsung Galaxy Tab
)
So guys, please help me, Why I am getting a blank value in the previous case ?
Edit
vardump($value) is returning :
string '{"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab
"}' (length=86)
Upvotes: 0
Views: 1039
Reputation: 2535
A bit weird but it may have to do with the database not giving you UTF8 strings (which json_decode needs).
Try
mysql_query("SET NAMES 'utf8';");
before your actual query.
//edit: The linebreak is actually causing the problem. try with the json validator: http://jsonlint.com/
Upvotes: 1
Reputation: 16111
string '{"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab
"}' (length=86)
There is your problem. That linebreak and tab. It should look like:
string '{"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab"}' (length=84)
This is why var_dump()
outputs the length, so you can validate against any unwanted/invisible characters.
You can store line breaks in javascript strings as \n
.
json_encode()
will do this automatically.
This applies for all escape sequences (commonly used ones are \n
, \r
, \t
and \b
).
Upvotes: 0
Reputation: 151
try to get it in utf8.
From the json_decode doc ( This function only works with UTF-8 encoded data. )
Upvotes: 1