我正在使用PHP /库MySQLi两人准备语句来获取一个MySQL数据库中的数据。 然而,当我运行报表,我得到的“命令不同步,你不能运行命令现在”的错误。
这里是我的代码:
$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();
我已经试过:
PS:在“不同步”的错误来源于第二条语句的“$ stmt1->错误”
在mysqli的查询::如果使用MYSQLI_USE_RESULT所有后续调用将返回错误命令不同步的,除非你调用mysqli_free_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的文档中找到。 阅读这些细节清楚地表明,在结果集准备语句的执行需要在同一个连接上执行的另一准备语句之前完全取出。
固定的问题可以通过使用存储结果调用来完成。 下面是我最初试图做一个例子:
<?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);
?>
上面的代码将导致以下错误:
注释语句错误:命令不同步; 你现在不能运行这个命令
PHP注意:未定义的变量:在POST_TITLE上error.php线41 ID:9033 - >阵列()
这是需要做的,使其正常工作的内容:
<?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->store_result();
$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);
}
}
$post_stmt->free_result();
}
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);
?>
有两件事情需要注意上面的例子:
The bind and fetch on the statement still works correctly.
Make sure the results are freed when the processing is done.
对于那些你们谁做正确的事,并使用预处理语句的存储过程。
出于某种原因,使用一个输出变量如在存储过程的参数时的mysqli不能释放资源。 为了解决这个问题只是在过程的主体内返回一个记录而不是一个输出变量/参数存储的价值。
例如,代替的具有被设置OutputVar中= LAST_INSERT_ID(); 你可以有SELECT LAST_INSERT_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;
}
如果要返回多条记录与存储过程(你有多个选择)设置,然后确保使用$ stmt-> next_result要经过所有这些记录集();