2

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.

0

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.