skip to Main Content

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:

enter image description here

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);

?>

2

Answers


  1. 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.

    Login or Signup to reply.
  2. You use mysqli 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search