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?
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.
}
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;
}
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.
mysqli::fetch_all()
needs mysqlnd
driver to be installed before you can use it.
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.
As an addition to Fabrizios answer, please consider building up your array with a loop:
$array = array();
while($row = $result->fetch_assoc())
$array[] = $row;
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
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With