1

My script was working fine on my localhost and when I uploaded it to my online host, I get a Call to undefined method mysqli::mysqli_fetch_all() error. I read up online and found out it has to do with a missing MySQLnd driver.

My host uses c panel and I cannot install any extensions. My local php version is 5.5.12 and the one on my hosting server is 5.6.26 and the MySQL version is 5.5.52-cll.

After reading up, I changed the fetch_all() to fetch_assoc(), the error went away but the DB only returns one result even though I am using it in a loop. What am I doing wrong?

if (!$errors) {
    if ($result = $mysqli->query($addresult)) {
        while ($row = $result->fetch_all()) {
            $returnResult = $row;
        }
    }
}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Show us the code where you're using `fetch_assoc()`. – Rajdeep Paul Dec 10 '16 at 09:56
  • 2
    `fetch_all()` requires a very specific driver be installed called `mysqlnd`. We have to assume your host has not installed that driver. As far as I know that is the only mysqli function that will cause you problems. http://php.net/manual/en/mysqli-result.fetch-all.php – RiggsFolly Dec 10 '16 at 10:04
  • what if you start working with PDO.. – SAR Dec 10 '16 at 10:15
  • Please stop using PHP 5. It hasn't been supported for years. – Dharman Feb 07 '20 at 07:44

3 Answers3

1

fetch_all gathers the whole result set into one big multidimensional array. fetch_assoc gathers each row into a one-dimensional array.

So you need to loop through adding the data to an array in order to get the full set. You are currently just redefining the variable each time you go through the loop, so it will always contain the last value.

What you need will look like:

$returnResult = []; //initialise empty array
while($row = $result->fetch_assoc())
{
    $returnResult[] = $row;
}
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0

Actually you are overwriting your variable $returnResult again and again in loop,do like below:-

$returnResult = array(); // array variable
if(!$errors) {        
   if( $result = $mysqli->query($addresult) ) {
      while($row = $result->fetch_assoc()) // use fetch_assoc here
      {
        $returnResult[] = $row; // assign each value to array
      }
   }
}

Note:- i saw your jquery code in comment,so just change one line in above code:-

$returnResult[] = array_values($row);
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
  • Thanks! I am using jquery and Ajax to display the result on my page, with it now being an array, what do I need to change in my jquery? I was using JSON encode in my previous code to output the $row. My jquery looked like `$.each(response.result, function( index, value) { $('#divcontent').append('

    '+value[0]+' '+value[1]+':'+value[2]+ ' '+value[3]+'

    '); });`
    –  Dec 10 '16 at 10:04
  • Ah I see, that makes sense. Thank you –  Dec 10 '16 at 10:06
  • 1
    @Bruno you can change like this:- `$returnResult[] = array_values($row);`check this one first. – Alive to die - Anant Dec 10 '16 at 10:07
  • Awesome! `$returnResult[] = array_values($row);` worked perfectly! I see the logic behind it now, thank you so much Anant –  Dec 10 '16 at 10:10
0

This can also happen if the $link / $con / $mysqli connection is not alive (nullified).

Check whether the connection has been established.

In my case i was using required_once('link.php') two times on a single page.

On the second call this error was thrown. Here, instead of required_once when i used required('link.php'), the connection was established and this error was resolved.

urkoruche
  • 97
  • 1
  • 6
sifr_dot_in
  • 3,153
  • 2
  • 33
  • 42