【问题标题】:Alternate function to replace mysqli_stmt::get_result替换 mysqli_stmt::get_result 的替代函数
【发布时间】:2015-05-19 20:41:43
【问题描述】:

我的应用程序使用 MySQL 准备语句在 localhost 中构建,如下所示:

include('db.php');

$stmt = $mysqli->prepare("SELECT * FROM `hero` WHERE name=? LIMIT 1") or die($mysqli->error);

$stmt->bind_param('s', $name);

$name = 'superman';

$stmt->execute();

$check = $stmt->get_result();
$rows = $check->fetch_object();

$num = $check->num_rows;

if($num == 0){
    echo 'Failed!';
}else{
    echo 'Found!';
}

查询在本地主机中运行良好,当我在虚拟主机中测试时显示错误:

致命错误:调用未定义的方法 mysqli_stmt::get_result()...

经过一些网络研究,发现这是由于主机中未安装mysqld 驱动程序(PHP 版本 5.4++,我认为 5.3 以上版本已经可用?)。

是否有任何解决方法可以使用替代方式而不是 mysqli_stmt::get_result

【问题讨论】:

  • 应该与驱动没有太大关系。 get_result 自 5.3.0 以来一直在 php 中...
  • 不知您是否需要安装php5-mysqlnd才能使用此功能?

标签: php mysqli prepared-statement


【解决方案1】:

我遇到了同样的问题。您可以联系您的托管服务提供商或找到解决此问题的方法。

mysqlnd 在 PHP 5.3 的源代码中,但未编译所有发行版,它是 5.4+ 的默认值

建议的解决方法: 您可以尝试 mysqli_stmt_bind_result 或完全替换用 PDO 准备的 mysqli

【讨论】:

    【解决方案2】:

    (警告:我使用的是 PHP 7.x,所以这可能适合您,也可能不适合您!)

    我知道有人谈论“mysqli_stmt_get_result”不起作用。 我遇到过这个问题!参考这个链接:PHP Fatal error: Call to undefined function mysqli_stmt_get_result()

    (见杰夫的回答)。 我的主机有 mysqlnd 驱动程序,但没有 nd_mysqli 驱动程序。 这可能是相关的。我不确定。

    我想我找到了替代方法

    // assumes execution of a prepared statement
    // for example: "SELECT * FROM table WHERE important_field LIKE ?"
    
    $result = mysqli_stmt_get_result($stmt);
    $row = mysqli_fetch_array($result,MYSQLI_NUM);
    

    我对数组和引用做了一些奇怪的事情。

    $row = array();
    $references_array2 = array();
    for($i=0; $i<$stmt->field_count; $i++){
        $row[] = "";
        $references_array2[] = &$row[$i];
    }
    array_unshift($references_array2,$stmt);
    $status = call_user_func_array("mysqli_stmt_bind_result",$references_array2);
    

    每当我想填充行数组时,我都会调用:

    $fetch_status = mysqli_stmt_fetch($stmt);
    

    我根据文档中的规则在 while 循环或 if 语句中检查 fetch_status 变量: https://www.php.net/manual/en/mysqli-stmt.fetch.php

    我建议在与 NULL 或 FALSE 进行比较时使用三等号运算符 ("===") 以避免两个值(和类型)之间的任何混淆。

    if ($fetch_status === NULL){
        // No more rows/data exists or data truncation occurred
    
    } elseif ($fetch_status === FALSE) {
        // Error occurred
    
    } // more code, etc.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-31
      • 2014-09-14
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多