8

I have a php-mysqli code that works find one my local server but on using it on my server i am getting a

Fatal error: Call to undefined function mysqli_fetch_all() in /home3/t561257/public_html/admin/database.php on line 49

The following part of the code is where the problem is.

 function fetch_rows($queryname) {
        $result = $this->connection->query($queryname);
        $row = mysqli_fetch_all($result, MYSQLI_ASSOC);
        return $row;        
    }

I use it in the following manner

 $next_four_rows = $db_link->fetch_rows($query_four_latest);

$db_link is the class which has the method fetch_rows.

I am using php 5.5 on my local server where as the server is running 5.4.27 I am really clueless on how to fix it

Bazinga777
  • 5,140
  • 13
  • 53
  • 92

2 Answers2

20

If mysqli_fetch_all is not available because your PHP installation was not compiled with mysqlnd, you have two options:

  1. Recompile PHP with mysqlnd or possibly install another specific package from your Linux distribution's package repository.

  2. Use a simple loop:

     $data = [];
     while ($row = $result->fetch_assoc()) {
         $data[] = $row;
     }
    

You could even create a compatibility fallback, without needing to change all your code:

if (!function_exists('mysqli_fetch_all')) {
    function mysqli_fetch_all(mysqli_result $result) {
        $data = [];
        while ($row = $result->fetch_assoc()) {
            $data[] = $row;
        }
        return $data;
    }
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
deceze
  • 510,633
  • 85
  • 743
  • 889
1

Don't look for alternatives. While mysqli_fetch_all() can be easily polyfilled as demonstrated by deceze, this function should be available on all installations. It may be unavailable only on very old PHP versions or if PHP was compiled with libmysql. In PHP 8.1, it's available even when compiled against libmysql, which means that as of PHP 8.1 it's impossible not to have this function!

You should not be using PHP which doesn't have this function. If you somehow find yourself missing it, then the reasonable thing to do is to fix your PHP installation. On cPanel, the PHP extension configuration can be confusing and results in a lot of users mistakingly enabling mysqli extension instead of nd_mysqli. On a normal PHP installation, there is no nd_mysqli extension and mysqli is compiled with the native driver by default. As of PHP 8.2, compiling with libmysql is no longer supported and you should only have one mysqli extension to choose from.

Dharman
  • 30,962
  • 25
  • 85
  • 135