【发布时间】:2022-01-25 12:07:59
【问题描述】:
我有以下代码:
$post_title = "Some dynamic POST data";
$stmt = $conn->prepare("SELECT * FROM `posts` WHERE title=? ");
$stmt->bind_param("s", $post_title);
$stmt->execute();
$rows = $stmt->get_result()->num_rows;
$stmt->get_result()->free_result(); // throws error: commands out of sync
$stmt = $conn->prepare("... some new condition");
$stmt->bind_param(...);
$stmt->execute();
$rows = $stmt->get_result()->num_rows;
我知道我可以简单地使用 $stmt->free_result,但是 php 文档 https://www.php.net/manual/en/mysqli-result.free.php 提到您也可以在 mysqli_result 对象上使用 free_result,所以我们为什么不能在 mysqli_stmt->get_result() 上使用它,这是一个结果对象也是?
【问题讨论】:
-
也可以查看stackoverflow.com/questions/14554517/…等其他问题
-
@Progman 你的链接没有解释
get_result。 -
您使用了两次
$stmt->get_result()。你为什么这样做? -
@Progman 因为第一次使用我需要得到
num_rows,第二次我想使用free_result,这样我就可以在$stmt中使用我的第二个查询。文档没有提到get_result刷新结果,这是我认为正在发生的事情,因为不使用free_result允许我运行第二个查询