简体   繁体   中英

How to return array of object to ajax call in php

I have a php to return data to ajax callback.

if((isset($_GET['keyword'])) && (strlen($_GET['keyword']) > 3)){
   $query = "SELECT * FROM bin";
   $result = $db->run_query($conn,$query);
   while ($rows = mysqli_fetch_array($result,MYSQLI_NUM)){
      $data[] = $rows;
   }      
   echo json_encode($data);
}

But when I log it result from it, my data is array of array. I want my return data is array of object.

How to do it? Thanks.

use mysqli_fetch_object . It will return rows as object. You can store those objects in an array.

while ($obj = mysqli_fetch_object($result)) {
    $data[] = $obj;
}

mysqil_fetch_object() is similar to mysqli_fetch_array() , with one difference - an object is returned, instead of an array.

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