2013-08-28 12:45
浏览 246


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>";
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>";
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 ?


vardump($value) is returning :

string '{"image":"upload_1182341221.jpg","conttxt":"Get a chance to win Samsung Galaxy Tab
    "}' (length=86)

图片转代码服务由CSDN问答提供 功能建议

我正在处理 facebook app 。 我已经以json格式在数据库中存储了一些值。

当我在目标页面上获取值并尝试显示如下值时: < pre> $ sql = mysql_query(“select * from`afterpublish` where`tabid` ='$ page_id'”)或die(mysql_error()); $ sql = mysql_fetch_assoc($ sql); \ n $ contest_id = $ sql ['contestid']; $ temp_id = $ sql [“tempid”]; $ value = $ sql [“values”]; $ returnValue = json_decode($ value); \ necho“&lt; pre&gt;”; print_r($ returnValue); echo“&lt; / pre&gt;”;


$ value = {“image”:“upload_1182341221.jpg”,“conttxt”:“有机会赢取三星Galaxy Tab”} \ n

我将 $ returnValue 视为空白。


   $ returnValue = json_decode('{“image”:“upload_1182341221.jpg”,“conttxt”:“有机会赢取三星Galaxy Tab”}); 
echo“&lt; pre&gt;”; 
print_r($ returnValue)  ; 
echo“&lt; / pre&gt;”; 

然后$ returnValue是retu rning:

  stdClass Object 
 [image] =&gt;  upload_1182341221.jpg 
 [conttxt] =&gt; 有机会赢得三星Galaxy Tab 



vardump($ value)正在返回:

  string  '{“image”:“upload_1182341221.jpg”,“conttxt”:“有机会赢取三星Galaxy Tab 
”}'(长度= 86)
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doucai1901 2013-08-28 12:59
    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 .
    json_encode() will do this automatically.

    This applies for all escape sequences (commonly used ones are , , \t and \b).

    解决 无用
    打赏 举报

相关推荐 更多相似问题