简体   繁体   English

php 5.2.6 的 mysqli_stmt_get_result 替代方案

[英]mysqli_stmt_get_result alternative for php 5.2.6

I am not an expert of php, I developed a small service which query a mysql db.我不是 php 专家,我开发了一个查询 mysql 数据库的小服务。

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.但是我用 php 5.4 开发,然后发现我的网络托管计划有 5.2.6,所以我对一些未定义的函数几乎没有问题。

Specifically, in this case, how can I solve the mysqli_stmt_get_result undefined function available on > 5.3 ?具体来说,在这种情况下,如何解决 > 5.3 上可用的 mysqli_stmt_get_result 未定义函数? 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.关键是我使用的是准备好的语句,由于我缺乏 php 知识,我发现没有其他方法可以在不重写整个脚本的情况下解决问题。

I thought that some of you may have a simple and easy solution.我认为你们中的一些人可能有一个简单易行的解决方案。

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.顺便说一句 - mysqlnd 在 5.3 中可用,但必须在 5.4 中编译,默认情况下就在那里。

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;
    ...
}

The mysqli_stmt_get_result function is PHP 5.3 or greater only. mysqli_stmt_get_result函数仅适用于 PHP 5.3 或更高版本。 It does not exist for your PHP 5.2.x version (which is not supported any longer btw.).它不存在于您的 PHP 5.2.x 版本(顺便说一句,不再受支持)。

The alternative is to use mysqli_stmt_bind_result with variable bindings.另一种方法是将mysqli_stmt_bind_result与变量绑定一起使用。

In your concrete example this has even the benefit that you do not need to assign the array members to variables, because you can bind the variables directly.在您的具体示例中,这甚至具有您不需要将数组成员分配给变量的好处,因为您可以直接绑定变量。

The mysqli_stmt_get_result function was introduced because someone thought this would stand in your way and getting an array would have been easier.引入mysqli_stmt_get_result函数是因为有人认为这会妨碍您并且获取数组会更容易。

If you are in a situation where you aren't allowed to add mysqlnd, this shows how to get data into an an associative array without mysqli_stmt_get_result().如果您处于不允许添加 mysqlnd 的情况,这将显示如何在没有 mysqli_stmt_get_result() 的情况下将数据放入关联数组中。 Mysqli - Bind results to an Array Mysqli - 将结果绑定到数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM