mysqli fetch_all() not a valid function?
Asked Answered
F

11

52

Thanks to the answers I have figured out that I am unable to use fetch_all() because i am using PHP 5.2.17 - fetch_assoc with while loop worked.


The function I am using fetch_all is coming back with this error:

Fatal error: Call to undefined method mysqli_result::fetch_all() in

$mysqli = new mysqli($host, $username, $password, $database);
$query = "LONG QUERY that works, tested in phpmyadmin"
$result = $mysqli->query($query);
$result->fetch_all(); or  $mysqli->fetch_all() tried both
mysqli_fetch_all() was already tried.
$mysqli->close(); 

I am able to connect to the DB and I have pulled single rows. When I place the query in PHPMYADMIN I get 5 rows back.

Does this function even work? Is there a way I can place my data into an assoc array on my own?

Foretopmast answered 14/7, 2011 at 14:4 Comment(0)
J
56

This function is available since PHP 5.3.0. Possibly your version is older. Use fetch_assoc() instead.

while ($row = $result->fetch_assoc()) {
    // do what you need.
}
Jactitation answered 14/7, 2011 at 14:16 Comment(0)
D
34

mysqli::fetch_all() needs mysqlnd driver to be installed before you can use it.

Decuple answered 21/10, 2014 at 23:7 Comment(1)
Yes, and if you have a roundcube running (on Debian) it will stop working since mysqlnd removes php-mdb2-driver-mysql and roundcube-mysql driver. So install mysqlnd with cautionLema
A
22

Issue seems to be 'mysqli' and 'mysqlnd' are not working together. I had the same issue in cpanel

Steps:

1) Go to CPanel dashboard

2) Go to 'Select PHP Version', you should see a bunch of checkboxes

3) Set PHP version (5.4 or above)

4) Uncheck the 'mysqli' extension and check both 'mysqlind' and 'nd_mysqli'

Apparently 'nd_mysqli' is 'mysqli' but configured to work with 'mysqlind' and to make it work you have to uncheck 'mysqli' due to settings conflicts.

Following answer I found here helped me.

Aardwolf answered 19/6, 2019 at 23:29 Comment(5)
this is the correct answer. it's not about the PHP version, but about the extension. must be nd_mysqli and mysqlind. my hosting pre-select mysqli and nd_mysqli, that's why mysqli_fetch_all does not work.Mccusker
Correct answer! Thanks a lot, I do not know why this happened but it helped me ^^Ghats
Correct! Thanks a lot!!Gabion
Don't just change this setting on a whim, it can have serious implications. Doing so took down my site until I reverted the changes.Luettaluevano
corect thanks alot it worked for me as wellGilly
I
21

The typical way to construct an associative array is in a while loop:

$results_array = array();
$result = $mysqli->query($query);
while ($row = $result->fetch_assoc()) {
  $results_array[] = $row;
}
Interlocutrix answered 14/7, 2011 at 14:17 Comment(1)
This code helped me solve my problem. Thank you!Boulanger
R
9

Please be careful about using fetch_all():

http://www.php.net/manual/en/mysqli-result.fetch-all.php#88031

Read the note about mysqldn requirement.

Roulade answered 14/7, 2011 at 14:13 Comment(1)
Couldn't find anything about the mysqldn requirement on that page. Could you elaborate, why fetch_all() is "evil"? ThanksCanton
H
8

As an addition to Fabrizios answer, please consider building up your array with a loop:

$array = array();
while($row = $result->fetch_assoc())
    $array[] = $row;
Horseman answered 14/7, 2011 at 14:16 Comment(0)
H
3

If you don't want to iterate using Karolis' answer, use

     while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
    {
       $rows[] = $row;
    }

to achieve the same result as

$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

You may change constant to MYSQLI_NUM or MYSQLI_BOTH. This optional parameter are constants indicating what type of array should be produced from the current row data.

Notice that mysqli_fetch_array($result,MYSQLI_ASSOC ) behaves identically to the mysqli_fetch_assoc() function, while mysqli_fetch_array($result,MYSQLI_NUM) will behave identically to the mysqli_fetch_row() function. The final option MYSQLI_BOTH will create a single array with the attributes of both. http://php.net/manual/en/mysqli-result.fetch-array.php

Hyperboloid answered 24/3, 2018 at 10:10 Comment(2)
This answer is essentially wrong. mysqli_fetch_array($result, MYSQLI_ASSOC); does not achieve the same result as $rows = mysqli_fetch_all($result, MYSQLI_ASSOC);Hickerson
It's funny I didn't notice that. Grateful @YourCommonSenseHyperboloid
A
2

I had 5.6 but didn't work for me anyway. By enabling the mysqlnd driver it worked for me. Just write apt-get install php5-mysqlnd then restart php and you're good to go!

Alternately answered 25/3, 2017 at 2:30 Comment(0)
C
0

PHP Fatal error: Call to undefined function mysqli_fetch_all()

$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);//not work

change to:php 5.3 or

$query = "SELECT * FROM `user_counter` ORDER BY id DESC LIMIT 20";
$result =  mysqli_query($connection, $query) or die
("Error in Selecting " . mysqli_error($connection));
$rows = array();
while ($row = $result->fetch_assoc()) {
    $rows[] = $row;
}
echo json_encode($rows);
Crystallize answered 6/2, 2016 at 19:32 Comment(0)
R
0

Just to add an additional note about the mysqlnd driver: It also has to have the mysqli API extension installed. Without the extension, fetch_all() still won't be available. I found this while troubleshooting why my code worked on one server but not another, both PHP > 5.3. Use phpinfo() to verify the installed extensions.

Riggs answered 15/4, 2016 at 15:52 Comment(0)
M
0
function runQuery($query) {
        $result = mysqli_query($this->conn,$query);
                $resultset  = array(); //you need to define array
                         while($row=$result->fetch_assoc()) {
                              $resultset[] = $row; //then insert result into the array
            }       
            if(!empty($resultset))
            {

            return $resultset;}
            else{return 'empty';};
    }
Menfolk answered 2/3, 2017 at 4:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.