Mysqli_stmt_get_result alternative for php 5.2.6

I am not a php expert, I have developed a small service that queries the mysql db.

However, I developed php 5.4 and then found my web hosting plan is 5.2.6, so I have few problems with some undefined function.

Specifically in this case, how can I solve the mysqli_stmt_get_result undefined function available in> 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 thing is, I am using prepared statement, due to lack of php knowledge, I have not found another way to solve the problem without rewriting the whole script.

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

+1


source to share


3 answers


The function mysqli_stmt_get_result

only has PHP 5.3 or more. This does not exist for your PHP 5.2.x version (which is no longer supported by btw.).

An alternative is to use mysqli_stmt_bind_result

with variable bindings.



In your particular example, this even has the advantage that you don't need to assign the array members to variables, because you can bind variables directly.

The function mysqli_stmt_get_result

was introduced because someone thought it would get in your way and getting an array would be easier.

0


source


There was a similar problem. BTW - mysqlnd is available since 5.3 but needs to be compiled. 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'];
    ...
}

      



from

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

      

+6


source


If you are in a situation where you are not allowed to add mysqlnd, this shows how to get data into an associative array without mysqli_stmt_get_result (). Mysqli - Bind results to array

0


source







All Articles