dongxian6285 2014-10-08 15:46
浏览 34
已采纳

PHP 5.5升级破坏了这个简单的阵列/ JSON编码器

I just upgraded to 5.5.17 from 5.3.x and the following code stopped working (I get a blank response).

$gestr = mysql_query("SELECT name,age,id FROM users");
$star = array();
while($starid = mysql_fetch_array($gestr)){
     $star[] = array('name'=>$starid['name'],'age'=>$starid['age'],'id'=>$starid['id']);
}
$final=array('users'=>$star);
echo json_encode($final);

Please note that I have simplified the SQL statement, but I know it works all the way through the while loop because I can echo out $star[1][name] and it prints a user name.

  • 写回答

1条回答 默认 最新

  • dtt5830659 2014-10-08 16:18
    关注

    The issue was with json_encode not being able to parse a non-UTF8 encoded name. Thanks to @ChrisForrence for suggesting using json_last_error();

    The error was fixed by adding utf8_encode();

    $star[] = array('name'=>utf8_encode($starid['name']),'age'=>$starid['age'],'id'=>$starid['id']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)