简体   繁体   中英

json encoded data response is empty

I am trying to pull the data from the sample code below (it is through cpanel). I wanted to pull it in JSON encoded format, but there is no data that is displaying. See this screenshot, and my code below it:

在此处输入图片说明

Can someone help me with the problem?

<?php
include "db.php";

$data=array();

$q=mysqli_query($con,"SELECT a.*,a.date_added AS date_added2,a.status AS entry_status,a.added_by AS entry_provider FROM entries a WHERE a.status = 'Approved' ORDER BY a.id DESC") 
or die(mysql_error());

while ($row=mysqli_fetch_object($q)){
    $data[]=$row;
}

echo json_encode($data);

?>

You use mysql i to get the results, but mysql without i to print error message. These are different extensions, so if there's an error in your mysqli_query call, then mysql_error returns empty string and execution stops. Which is what you see in the browser.

One possibility is that your $result is empty.

AND

The mysqli_fetch_object returns you an object not array.

You can use this code to sort out your problem

$q=mysqli_query($con,"SELECT a.*,a.date_added AS date_added2,a.status AS entry_status,a.added_by AS entry_provider FROM entries a WHERE a.status = 'Approved' ORDER BY a.id DESC") 
or die(mysqli_error($con));

while ($row=mysqli_fetch_assoc($q)){
    array_push($data,$row);
}

echo json_encode($data);

This can definitely help you.

check that the data that you encode is in utf8

If your database is in latin1, you may need mysqli_set_charset()

check json_last_error_msg()

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