【发布时间】:2020-06-23 16:12:21
【问题描述】:
我使用准备好的语句执行了一个查询,现在我想在 while 循环中执行另一个查询。但它给了我一个错误Commands out of sync; you can't run this command now。如何解决这个问题?
<?php
$query = "SELECT post_id, post_title, post_author, post_date, post_image, post_content, post_tags FROM posts WHERE post_status = ? LIMIT ?, ?";
$stmt = mysqli_prepare($connection, $query);
if (!$stmt) {
echo "Mysql error";
} else {
mysqli_stmt_bind_param($stmt, "sii", $db_post_status, $page_1, $per_page);
$db_post_status = "Published";
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $post_id, $post_title, $post_author, $post_date, $post_image, $post_content, $post_tags);
}
while (mysqli_stmt_fetch($stmt)) {
?>
//// Some HTML code ////
<?php
$query = "SELECT like_count FROM likes WHERE like_post_id = $post_id ";
$sendQuery = mysqli_query($connection, $query);
$likes = mysqli_num_rows($sendQuery);
?>
//// Some HTML code ////
<?php } mysqli_stmt_close($stmt); ?>
【问题讨论】:
-
你的哪一行代码失败了?
-
@PaulSpiegel,我的第二个查询没有执行?
-
见:Buffered and Unbuffered queries。但我认为您可以通过单个查询获得相同的结果。第二个查询中的
$post_id是什么? -
@PaulSpiegel
$post_id是从第一个查询中获取的,它不能是静态的,所以我需要在第一个查询中执行第二个查询。但是如何在准备好的语句中启用缓冲?
标签: php mysql mysqli prepared-statement