PHP命令不同步错误
我在PHP/MySQLi中使用两个准备好的语句从mysql数据库检索数据。但是,当我运行这些语句时,会出现“命令不同步,您现在无法运行命令”错误 这是我的密码:PHP命令不同步错误,php,mysql,mysqli,Php,Mysql,Mysqli,我在PHP/MySQLi中使用两个准备好的语句从mysql数据库检索数据。但是,当我运行这些语句时,会出现“命令不同步,您现在无法运行命令”错误 这是我的密码: $stmt = $mysqli->prepare("SELECT id, username, password, firstname, lastname, salt FROM members WHERE email = ? LIMIT 1"; $stmt->bind_param('s', $loweredEm
$stmt = $mysqli->prepare("SELECT id, username, password, firstname, lastname, salt FROM members WHERE email = ? LIMIT 1";
$stmt->bind_param('s', $loweredEmail);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($user_id, $username, $db_password, $firstname, $lastname, $salt);
$stmt->fetch();
$stmt->free_result();
$stmt->close();
while($mysqli->more_results()){
$mysqli->next_result();
}
$stmt1 = $mysqli->prepare("SELECT privileges FROM delegations WHERE id = ? LIMIT 1");
//This is where the error is generated
$stmt1->bind_param('s', $user_id);
$stmt1->execute();
$stmt1->store_result();
$stmt1->bind_result($privileges);
$stmt1->fetch();
我所尝试的:
- 将准备好的语句移动到两个单独的对象
- 使用代码:
while($mysqli->more_results()){ $mysqli->next_result(); } //To make sure that no stray result data is left in buffer between the first //and second statements
- 使用free_result()和mysqli_stmt->close()
PS:“不同步”错误来自mysqli::query中的第二条语句“$stmt1->error”,如果使用mysqli\u use\u RESULT,则所有后续调用都将返回不同步的错误命令,除非调用mysqli\u free\u RESULT() 调用多个存储过程时,可能会遇到以下错误:“命令不同步;现在无法运行此命令”。 即使在调用之间对结果对象使用close()函数,也可能发生这种情况。 要解决此问题,请记住在每次存储过程调用之后调用mysqli对象上的next_result()函数。见下例:
<?php
// New Connection
$db = new mysqli('localhost','user','pass','database');
// Check for errors
if(mysqli_connect_errno()){
echo mysqli_connect_error();
}
// 1st Query
$result = $db->query("call getUsers()");
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
$user_arr[] = $row;
}
// Free result set
$result->close();
$db->next_result();
}
// 2nd Query
$result = $db->query("call getGroups()");
if($result){
// Cycle through results
while ($row = $result->fetch_object()){
$group_arr[] = $row;
}
// Free result set
$result->close();
$db->next_result();
}
else echo($db->error);
// Close connection
$db->close();
?>
我希望这将有助于“命令不同步;您现在无法运行此命令”
有关此错误的详细信息可以在mysql文档中找到。阅读这些细节可以清楚地看出,在同一连接上执行另一条准备好的语句之前,需要完全获取一条准备好的语句执行的结果集
通过使用store result调用可以解决问题。下面是我最初尝试做的一个例子:
<?php
$db_connection = new mysqli('127.0.0.1', 'user', '', 'test');
$post_stmt = $db_connection->prepare("select id, title from post where id = 1000");
$comment_stmt = $db_connection->prepare("select user_id from comment where post_id = ?");
if ($post_stmt->execute())
{
$post_stmt->bind_result($post_id, $post_title);
if ($post_stmt->fetch())
{
$comments = array();
$comment_stmt->bind_param('i', $post_id);
if ($comment_stmt->execute())
{
$comment_stmt->bind_result($user_id);
while ($comment_stmt->fetch())
{
array_push($comments, array('user_id' => $user_id));
}
}
else
{
printf("Comment statement error: %s\n", $comment_stmt->error);
}
}
}
else
{
printf("Post statement error: %s\n", $post_stmt->error);
}
$post_stmt->close();
$comment_stmt->close();
$db_connection->close();
printf("ID: %d -> %s\n", $post_id, $post_title);
print_r($comments);
?>
对于那些做正确事情并使用存储过程和准备好的语句的人 由于某些原因,当在存储过程中将输出变量用作参数时,mysqli无法释放资源。要解决这个问题,只需在过程体中返回一个记录集,而不是将值存储在输出变量/参数中 例如,不设置outputVar=LAST_INSERT_ID();您可以选择最后一个插入ID();然后在PHP中,我得到如下返回值:
$query= "CALL mysp_Insert_SomeData(?,?)";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("is", $input_param_1, $input_param_2);
$stmt->execute() or trigger_error($mysqli->error); // trigger_error here is just for troubleshooting, remove when productionizing the code
$stmt->store_result();
$stmt->bind_result($output_value);
$stmt->fetch();
$stmt->free_result();
$stmt->close();
$mysqli->next_result();
echo $output_value;
while ($stmt->fetch()) {
echo $output_value;
}
现在,您可以执行第二个存储过程,而不会出现“命令不同步,现在无法运行命令”错误。如果在记录集中返回多个值,则可以按如下方式循环并获取所有值:
$query= "CALL mysp_Insert_SomeData(?,?)";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("is", $input_param_1, $input_param_2);
$stmt->execute() or trigger_error($mysqli->error); // trigger_error here is just for troubleshooting, remove when productionizing the code
$stmt->store_result();
$stmt->bind_result($output_value);
$stmt->fetch();
$stmt->free_result();
$stmt->close();
$mysqli->next_result();
echo $output_value;
while ($stmt->fetch()) {
echo $output_value;
}
如果要从存储的进程返回多个记录集(有多个选择),请确保使用$stmt->next_result()遍历所有这些记录集 +1问得好的问题。最近对于新用户来说似乎很少见。可能是@BackinaFlash的复制品我在写这篇文章之前已经看过这个问题了。不同之处在于,我使用mysqli_stmt::store_result()缓冲了我的结果,并且在尝试第二个语句之前释放了所有以前的结果。如果有可用的语句,您可以使用mysqli_ASYNC进行尝试,并查看它产生了什么输出。另外,您可能希望尝试在mysqli对象上运行free_result,而不是语句。@user191125找到解决方案了吗?我的代码看起来和你的代码几乎一模一样,我已经尝试了所有方法,但是第二次调用$mysqli->prepare时总是抛出“命令不同步”。请让我们更新您是如何解决此问题的?谢谢。我确实使用了“store_result()”我真的不明白为什么,但按照此答案中所示同时准备两个语句为我修复了它+1这是一个很好的答案,我也根据您的指示解决了我的问题。谢谢@G.N.Vashishtha。wordpress中的
wpdb
有什么问题吗。看这里