【问题标题】:Call to undefined method mysqli_stmt::next_result()调用未定义的方法 mysqli_stmt::next_result()
【发布时间】:2017-09-22 00:44:39
【问题描述】:

我正在尝试在 PHP 中循环通过预准备语句调用的存储过程。希望这不是不可能的。这是我的 PHP 代码:

$database = new mysqli($server, $user, $pass, $db);
$stmt = $database->prepare("CALL thisShouldReturnEightResultSets (?)");
$stmt->bind_param("i", $id);
$stmt->execute();

do {
    if ($res = $stmt->bind_result($myvar)) {
        $stmt->fetch();
        echo "*" . $myvar . "<br/>";
    } else {
        if ($stmt->errno) {
            echo "Store failed: (" . $stmt->errno . ") " . $stmt->error;
        }
    }
} while ($stmt->next_result());

应该这样打印:

* 4
* 16
* 7
etc...

... 然后在结果集用完后退出。相反,我得到:

* 4

Fatal error: Call to undefined method mysqli_stmt::next_result()

起初我尝试了 get_result 而不是 bind_result,但是出错了。基于this我发现我没有安装mysqlnd驱动。我用 bind_result 解决了这个问题。现在我需要解决 next_result 的问题,我猜这也是 mysqlnd 驱动程序的一部分。我在这里有什么选择?技术说明:

PHP Version 5.3.2-1ubuntu4.11

谢谢。

【问题讨论】:

  • next_result 仅在使用 multi_query 时使用。
  • 你没有执行多个查询,为什么你认为你需要使用next_result
  • 存储过程中有多个选择。

标签: php mysqli prepared-statement multiple-resultsets


【解决方案1】:

我认为您不能使用准备好的语句来执行此操作,因为您没有 MYSQLND 驱动程序。你必须以老式的方式进行,转义。

$id = $database->real_escape_string($id);
if ($database->multi_query("CALL thisShouldReturnEightResultSets ('$id')")) {
    do {
        if ($result = $database->store_result()) {
            while ($row = $result->fetch_row()) {
                echo "*" . $row[0] . "<br/>";
            }
        }
    } while ($database->next_result());
}

【讨论】:

  • 不起作用。被调用的存储过程中有多个选择。上面的代码只会从第一个选择的结果集中打印“4”。
  • 我已经更改了答案以显示如何使用multi_query
【解决方案2】:

bindResult 不喜欢多个结果。

使用 get_result() 然后 fetch_assoc 并存储在一个数组中, 然后使用 foreach 循环输出结果。

  $res = $stmt->get_result();
  $array[];

  while($x = $res->fetch_assoc()){
     $array[]=$x;

    }

【讨论】:

猜你喜欢
  • 2022-01-11
  • 2014-12-23
  • 1970-01-01
  • 2023-04-10
  • 2014-01-09
相关资源
最近更新 更多