0

I'm starting to learn PHP and ran into something that I don't know how to debug...

I have two tables in my db, with following structures

wp_ad_management:

| id | template_id | date_from | date_to | img_url | ad_url | created_at |

and wp_ad_templates:

| id | name | proportions |

Here is my PHP to get data from them:

switch ($_GET['action']) {
  case 'all_ads':
    $result = mysqli_query($conn, "SELECT * FROM `wp_ad_management`");
    break;
  case 'all_templates':
    $result = mysqli_query($conn, "SELECT * FROM `wp_ad_templates`");
}

$data = array();
while ($row = mysqli_fetch_object($result)) {
  array_push($data, $row);
}

echo json_encode($data);

https://trevim.online/anuncios/php/get.php?action=all_ads everything is returned as expected https://trevim.online/anuncios/php/get.php?action=all_templates returns 200 but says no response data is available for this request.

I'm stuck on this for hours, reviewing all the code to try to understand what the difference is between one and the other! Please tell me how can one go about debugging this? There's no errors, nothing, just an empty response, when in fact I know that there are 6 entries in that templates table, which are returned if I run the same SQL directly in PHPMyAdmin.

Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

-2

Use mysqli_fetch_array to fetch a result row as an associative array.

switch ($_GET['action']) {
  case 'all_ads':
    $result = mysqli_query($conn, "SELECT * FROM `wp_ad_management`");
    break;
  case 'all_templates':
    $result = mysqli_query($conn, "SELECT * FROM `wp_ad_templates`");
}
$data = array();
while ($row = mysqli_fetch_array($result)) {
  array_push($data, $row);
}
echo json_encode($data);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Niraj Kumar
  • 38
  • 1
  • 5