5

While developing on localhost via XAMPP I used mysqli_fetch_all in my code. But after uploading on godaddy shared hosting, its not working.

I researched on internet and found out server should use MySQLnd to run mysqli_fetch_all. So I can't run my current code on server.

I need exact alternative of this code. Any suggestions?

Current code:

$result = mysqli_query($con,$query);
$arr = mysqli_fetch_all($result, MYSQLI_ASSOC);

$totalrecords = count($arr);

$json = array('sEcho' => '1', 'iTotalRecords' => $totalrecords, 'iTotalDisplayRecords' => $totalrecords, 'aaData' => $arr);
echo json_encode($json );
Dharman
  • 30,962
  • 25
  • 85
  • 135
sk786
  • 394
  • 1
  • 4
  • 21

2 Answers2

7

If you can't use it because you don't have mysqlnd installed, then fetch it like the you would normally do with mysqli_fetch_assoc()

$arr = array();
$result = mysqli_query($con,$query);
$totalrecords = mysqli_num_rows($result);
while($row = mysqli_fetch_assoc($result)) {
    $arr[] = $row;
}

$json = array('sEcho' => '1', 'iTotalRecords' => $totalrecords, 'iTotalDisplayRecords' => $totalrecords, 'aaData' => $arr);
echo json_encode($json);
Kevin
  • 41,694
  • 12
  • 53
  • 70
2

I have faced the same problem with my host, and for less code refactoring I think the better way is to implement a similar function using mysqli_fetch_assoc() or mysqli_fetch_array(), returning the same as mysqli_fetch_all(), like:

public function mysqli_fetch_all_alt($result) {
    $select = array();

    while( $row = mysqli_fetch_assoc($result) ) {
        $select[] = $row;
    }

    return $select;
}

Then just perform a find-replace in your project.

  • 2
    a hint: you can always wrap a function definition in a condition with function_exists(). it will save you a find-replace in a project. – Your Common Sense Sep 15 '16 at 12:33