命令不同步;你现在不能运行这个命令 [英] Commands out of sync; you can't run this command now
问题描述
我正在尝试执行我的 PHP 代码,该代码通过 mysqli 调用两个 MySQL 查询,并收到错误命令不同步;您现在无法运行此命令".
I am trying to execute my PHP code, which calls two MySQL queries via mysqli, and get the error "Commands out of sync; you can't run this command now".
这是我正在使用的代码
<?php
$con = mysqli_connect("localhost", "user", "password", "db");
if (!$con) {
echo "Can't connect to MySQL Server. Errorcode: %s\n". Mysqli_connect_error();
exit;
}
$con->query("SET NAMES 'utf8'");
$brand ="o";
$countQuery = "SELECT ARTICLE_NO FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE % ? %";
if ($numRecords = $con->prepare($countQuery)) {
$numRecords->bind_param("s", $brand);
$numRecords->execute();
$data = $con->query($countQuery) or die(print_r($con->error));
$rowcount = $data->num_rows;
$rows = getRowsByArticleSearch("test", "Auctions", " ");
$last = ceil($rowcount/$page_rows);
} else {
print_r($con->error);
}
foreach ($rows as $row) {
$pk = $row['ARTICLE_NO'];
echo '<tr>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['USERNAME'].'</a></td>' . "\n";
echo '<td><a href="#" onclick="updateByPk(\'Layer2\', \'' . $pk . '\')">'.$row['shortDate'].'</a></td>' . "\n";
echo '<td><a href="#" onclick="deleterec(\'Layer2\', \'' . $pk . '\')">DELETE RECORD</a></td>' . "\n";
echo '</tr>' . "\n";
}
function getRowsByArticleSearch($searchString, $table, $max) {
$con = mysqli_connect("localhost", "user", "password", "db");
$recordsQuery = "SELECT ARTICLE_NO, USERNAME, ACCESSSTARTS, ARTICLE_NAME, date_format(str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s'), '%d %m %Y' ) AS shortDate FROM AUCTIONS WHERE upper(ARTICLE_NAME) LIKE '%?%' ORDER BY str_to_date(ACCESSSTARTS, '%d/%m/%Y %k:%i:%s')" . $max;
if ($getRecords = $con->prepare($recordsQuery)) {
$getRecords->bind_param("s", $searchString);
$getRecords->execute();
$getRecords->bind_result($ARTICLE_NO, $USERNAME, $ACCESSSTARTS, $ARTICLE_NAME, $shortDate);
while ($getRecords->fetch()) {
$result = $con->query($recordsQuery);
$rows = array();
while($row = $result->fetch_assoc()) {
$rows[] = $row;
}
return $rows;
}
}
}
我曾尝试阅读此内容,但我不确定该怎么做.我已经阅读了有关存储结果和免费结果的信息,但是在使用它们时这些没有任何区别.我不确定这个错误究竟是在哪一点引起的,想知道它是为什么引起的,以及如何解决它.
I have tried reading up on this, but I am unsure of what to do. I have read about store result and free result, however these have made no difference when using them. I am unsure at exactly which point this error is being caused, and would like to know why it is being caused, and how to fix it.
按照我的调试语句,由于靠近 '% 的我的 sql 语法错误,甚至没有输入 countQuery 的第一个 if 循环?%'
.但是,如果我只是选择 *
而不是尝试基于 LIKE 子句进行限制,我仍然会收到命令不同步错误.
Going by my debug statements, the first if loop for countQuery is not even being entered, because of an error in my sql syntax near near '% ? %'
. However if I just select *
instead of trying to limit based on a LIKE clause, I still get the command out of sync error.
推荐答案
你不能同时有两个查询,因为 mysqli 默认使用无缓冲查询(对于准备好的语句;它与 vanilla mysql_query
).您可以将第一个提取到数组中并循环遍历,或者告诉 mysqli 缓冲查询(使用 $stmt->store_result()
).
You can't have two simultaneous queries because mysqli uses unbuffered queries by default (for prepared statements; it's the opposite for vanilla mysql_query
). You can either fetch the first one into an array and loop through that, or tell mysqli to buffer the queries (using $stmt->store_result()
).
请参阅此处了解详情.
这篇关于命令不同步;你现在不能运行这个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!