gpt4 book ai didi

php - 命令不同步,即使第一个 SQL 查询不包含结果

转载 作者:行者123 更新时间:2023-11-29 00:07:47 28 4
gpt4 key购买 nike

我一直在阅读有关命令不同步的信息;你现在不能运行这个命令 问题已经有一段时间了,并且看到你不能留下任何未读的结果,这对我来说很有意义。但是,在以下情况下,我看不到要释放哪些结果。我从下面的 PHP 和 SQL 代码中删除了不相关的内容。

# Set local variables
$sql = "
SET @STARTDATE = '2014-09-01';
SET @RANK = 0;
";
if (mysqli_multi_query($conn, $sql)) {
# Success: do nothing else
} else {
# Failure: output the error message
echo "Error: " . $sql . "<br>" . $conn->error;
}

# Fetch and store the results
$sql = "
SELECT * FROM MyTable
";
$result = mysqli_query($conn, $sql);
if (!$result) {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

第二个查询(if (!$result) block )返回臭名昭著的 Commands out of sync 错误。如果我注释掉第一部分,第二个查询运行没有问题。如果我将第一个查询更改为只有一个 SET 语句而不是两个,则第二个查询运行没有问题。因此,似乎我必须清除第一部分中每个单独 SQL 语句的“成功标志”。它是否正确?如果是这样,应该怎么做?

编辑:确实,您似乎必须刷新其间的所有结果。在第 1 部分和第 2 部分之间添加以下行可解决问题。

while (mysqli_next_result($conn)) {;} // Flush multi_queries

我在 PHP 手册的用户评论中找到了这个解决方案:http://nl3.php.net/manual/en/mysqli.multi-query.php

最佳答案

很简单,你的第一个查询

SET @STARTDATE = '2014-09-01';
SET @RANK = 0;

将生成 2 个结果集,直到它们被处理为止,即使结果只是您无法继续的状态。

所以你需要做这样的事情:-

if (mysqli_multi_query($conn, $sql)) {
do {
/* unload result set */
if ($result = $mysqli->store_result()) {
// Check status
$result->free();
}
} while ($mysqli->next_result());
} else {
# Failure: output the error message
echo "Error: " . $sql . "<br>" . $conn->error;
}

当然你应该检查那个循环中的错误

关于php - 命令不同步,即使第一个 SQL 查询不包含结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26700418/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com