0

I want to get all the rows from a table and store them in an array. Whenever fetch_all is used, the page crashes without any error messages. All other fetch options (fetch_assoc, fetch_array, etc..) work.

Code snippet that calls getcourses.php:

 $.ajax({
      url: 'getcourses.php',
      success: function(data){
           //do stuff
      }
 });

getcourses.php:

<?php
  require('includes/db.php');

  $query = "SELECT * FROM course";

  if($result = $mysqli->query($query)){
    echo json_encode($result->fetch_all(MYSQLI_ASSOC));
    $result->close();
  }
  $mysqli->close();
?>

db.php:

$host = 'localhost';
$user = 'user';
$pass = 'pass';
$db = 'db'; 
$mysqli = new mysqli($host,$user,$pass,$db) or die($mysqli->error);

PHP version is 5.4.16 (CLI). The operating system is GNU. mysqlnd is installed.

This exact code does work on my ubuntu server with PHP version 7.2.24.

I am aware I can make a loop and use $result->fetch_assoc. However, getting this to work would be much better.

  • How many rows are you trying to fetch from the DB at once here? – Techie Jun 03 '20 at 04:33
  • You must get the error message first, then act. – Your Common Sense Jun 03 '20 at 04:41
  • 1
    By the way, 5.4.16 is **unacceptably** old. – Your Common Sense Jun 03 '20 at 04:43
  • I'm aware 5.4.16 is incredibly old, I'm not the one in control of the server. $mysql->error doesn't contain anything because the query isn't failing, just fetch_all. – twisteddragons Jun 03 '20 at 04:49
  • $mysql->error is not the only source of error messages. you must check web-server logs, configure PHP to log errors, do anything to get the error message, not just telling everyone $mysql->error is empty. Getting the error message is **the only** way to deal with your issue and no stranger can tell you it. Only your system can – Your Common Sense Jun 03 '20 at 05:04

0 Answers0