简体   繁体   English

使用while循环和json编码的数组索引在PHP中不起作用

[英]Array indexing using while loop and json encodeing not working in PHP

This code when run on our localhost works: 在我们的本地主机上运行时,此代码可以运行:

if($result){
     if($tmp=$result->num_rows){
         while($row = $result->fetch_assoc()) {
             $myArray[]  = $row;
         }
       echo json_encode($myArray[0]);
     }
}

But, when it is run on the server (Godaddy Hosting) it reutrns a null value. 但是,当它在服务器(Godaddy主机)上运行时,它将返回空值。

Check if you have any results in your database. 检查数据库中是否有任何结果。 If yes, then check if you have any non-ASCII caracters which makes your json_encode returning false, or encode them with utf8_encode : 如果是,则检查是否有任何非ASCII字符,使json_encode返回false,或使用utf8_encode对其进行编码:

while($row = $result->fetch_assoc()) {
      $myArray[]  = array_map('utf8_encode', $row);
}

You can also use json_last_error to help you debbuging. 您还可以使用json_last_error帮助调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM