Question

I just pushed something from my local machine to the live site and I get blank pages everywhere. I tracked down the problem to mysqli_fetch_all when I use that everything breaks. Why is it doing this? and how do I fix it? if I use mysqli_fetch_array or mysqli_fetch_row or mysqli_fetch_assoc it works but my data is not displayed properly.

This is one the functions that is breaking:

function getUserFields($connection){

        $query = mysqli_query($connection, "DESCRIBE `adminUsers`");
        $results = mysqli_fetch_all($query, MYSQLI_ASSOC);
        return $results;

}

The $connection is the connection to my database, I have confirmed that works and $query sting is getting populated, it just the mysqli_fetch_all does not work, how could I fix this in the function without adjusting how the data is displayed currently.

Was it helpful?

Solution

The function mysqli_fetch_all() and its object oriented counterpart (mysqli_result::fetch_all) are only available in PHP 5.3 and later.

It appears your server is running a lower version of PHP, or you're missing MySQL Native Driver (which this function depends on), which explains why the other fetch functions work and this doesn't.

From the Manual:

mysqli_fetch_all
mysqli_result::fetch_all
(PHP 5 >= 5.3.0)

For future debugging, turn errors on or refer to your error log because this will usually help to identify the problem.

error_reporting(E_ALL);
ini_set('display_errors', '1');

If you don't have access to upgrade the PHP version, or install MySQLND, you can simply convert the code to manually iterate using mysqli_fetch_assoc():

function getUserFields($connection){
    $query = mysqli_query($connection, "DESCRIBE `adminUsers`");    
    $results = array();

    if($query){
        while($row = mysqli_fetch_assoc($query)){
            $results[] = $row;
        }
    }

    return $results;
}

OTHER TIPS

As per php documentation, this function is mysqlnd only (MySQL native driver). If you do not have it installed on the server, it will not execute (and throw errors).

Available only with mysqlnd.

As mysqli_fetch_all() returns all the rows as an array in a single step, it may consume more memory than some similar functions such as mysqli_fetch_array(), which only returns one row at a time from the result set. Further, if you need to iterate over the result set, you will need a looping construct that will further impact performance. For these reasons mysqli_fetch_all() should only be used in those situations where the fetched result set will be sent to another layer for processing.

Visit http://www.php.net/manual/en/mysqli-result.fetch-all.php#refsect1-mysqli-result.fetch-all-mysqlnd for more information about this.

Consider using mysqli_fetch_array instead - http://www.php.net/manual/en/mysqli-result.fetch-all.php. It uses almost the same parameters, so there shouldn't be a problem.

There is a possibility that the problem lies elsewhere, but this would be my first guess (with no error reporting enabled yet :/)

EDIT: You will need to iterate over the results, because mysqli_fetch_array (and other functions from this family) returns one row at a time. This is probably why you have the results wrong when using them.

Other solution would be enabling php_mysqlnd.dll/php_mysqlnd.so extension in the php.ini on the server and restarting the service (you probably have in on the local machine, and that's why it works)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top