mysqli_stmt_get_result alternative for php 5.2.6

Leonardo picture Leonardo · Oct 21, 2012 · Viewed 11.6k times · Source

I am not an expert of php, I developed a small service which query a mysql db.

However I developed with php 5.4, and then discovered that my web hosting plan has 5.2.6, so I am having few problems with some undefined function.

Specifically, in this case, how can I solve the mysqli_stmt_get_result undefined function available on > 5.3 ? Here is the code:

  $stmt = mysqli_prepare($con,$db_query);

  if($stmt) {

     mysqli_stmt_bind_param($stmt,'ss',$after,$lang);
     mysqli_stmt_execute($stmt);
     $result = mysqli_stmt_get_result($stmt); // <-- getting undefined error here !!!

     $updated = array();
     $deleted = array();

     while($row = mysqli_fetch_assoc($result)) {

        if($row['status']==1) {
           array_push($updated,$row);
        } else {
           $cardName=$row['cardName'];
           $cardStatus=$row['status'];
           $cardId=$row['cardId'];
           $language=$row['language'];
           array_push($deleted,array(
                    'cardName'=>$cardName,
                                    'status'=>$cardStatus,
                                    'cardId'=>$cardId,
                                    'language'=>$language
                               )
           );
        }
     }

     $response = array(
        'cards'=>array(
           'updated'=>$updated,
           'deleted'=>$deleted
        )
     );

     $json = json_encode($response);
     mysqli_close($con);
     echo $json;

  }

The point is that I am using a prepared statement, due to my lack of php knowledge I found no other way of resolving the issue without rewriting the whole script.

I thought that some of you may have a simple and easy solution.

Answer

perNalin picture perNalin · Dec 10, 2012

Had a similar problem. BTW - mysqlnd is available with 5.3, but it has to be compiled in. 5.4, it's there by default.

In my case, I was able to keep most of my code and make it work by replacing the following

$result = mysqli_stmt_get_result($stmt); // <-- doesn't work without mysqlnd
while($row = mysqli_fetch_assoc($result)) {
    $cardName=$row['cardName'];
    ...
}

with

$stmt->bind_result($dbCardId, $dbCardName);  // <-- one param for each field returned
while ($stmt->fetch()) {
    $cardName = $dbCardName;
    ...
}