简体   繁体   中英

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.

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 :

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

You can also use json_last_error to help you debbuging.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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