Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysqli_fetch_all not working on shared hosting, need alternative [duplicate]

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 );
like image 997
sk786 Avatar asked Dec 10 '14 04:12

sk786


2 Answers

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);
like image 197
Kevin Avatar answered Sep 19 '22 18:09

Kevin


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.

like image 32
Leonardo Ortiz Avatar answered Sep 21 '22 18:09

Leonardo Ortiz