尝试执行多个准备好的语句时,Mysqli 返回错误 "Commands out of sync"
Mysqli returning error "Commands out of sync" when trying to do multiple prepared statements
我在 $noofspellschamps->bind_param('ss', $Patch_No, $champion 上收到错误 'Call to a member function bind_param() ';我已经测试了一些东西,这不是 Prepare 的问题,因为它在 phpmyadmin 上运行良好,我认为问题出在执行上,或者我可能需要先关闭来自 $champions 的流,但我需要它们,正如您在循环 我不太确定该怎么做。
EDIT2 是否有人知道问题所在并可以提供帮助?
$i=1;
$champion = array();
$noofspellschamp = array();
$Patch_No = trim($_GET['Patch_No']);
$champions = $conn->prepare("SELECT champion FROM champions where Patch_No = ?");
$champions->bind_param('s', $Patch_No);
$champions->execute();
//$champions->close();
$champions->bind_result($champ);
while($champions->fetch()){
$champion[$i]=$champ;
$noofspellschamps =$conn->prepare(
"SELECT Passive, Q, W, E, R,
((Passive != '') + (Q != '') + (W != '') + (E != '') + (R != '')
) as NumNotNull
FROM champions
WHERE Patch_No = ? AND Champion = ?");
$noofspellschamps->bind_param('ss', $Patch_No, $champion);
$noofspellschamps->execute();
$noofspellschamps->bind_result($noofspellsch);
while($noofspellschamps->fetch()){
$noofspellschamp[$i] = $noofspellsch['NumNotNull'];
echo $noofspellschamp[$i];
}
$i+=1;
}
编辑 2
你得到 "Commands out of sync error" 的原因是因为 mysql 客户端不允许你在仍然有行从以前的 in-进度查询。 Commands out of sync
使用提供的示例。 (可能需要根据自己的喜好进行更改)
$i=1;
$champion = array();
$noofspellschamp = array();
$Patch_No = trim($_GET['Patch_No']);
// query 1
$champions = $conn->prepare("SELECT champion FROM champions where Patch_No = ?");
$champions->bind_param('s', $Patch_No);
$champions->execute();
$result = $champions->get_result();
while($data = $result->fetch_assoc()){
// use data variable as an associative array
//query 2
$noofspellschamps = $conn->prepare(
"SELECT Passive, Q, W, E, R,
((Passive != '') + (Q != '') + (W != '') + (E != '') + (R != '')
) as NumNotNull
FROM champions
WHERE Patch_No = ? AND Champion = ?");
$noofspellschamps->bind_param('ss', $Patch_No, $data["champion"]);
$noofspellschamps->execute();
$result2 = $champions->get_result();
while($data2 = $result2->fetch_assoc()){
// other stuff with data2 variable
}
$i++;
}
As of PHP 5.3 mysqli_stmt::get_result
其中 returns 一个结果集对象。您可以使用 mysqli_result::fetch_array()
或 mysqli_result::fetch_assoc()
。但是,请注意,这仅适用于本机 MySQL 驱动程序。
本质上:
$stmt = $con->prepare("SELECT id FROM table1"); // first prepared statement
$stmt->execute();
$result = $stmt->get_result(); // Gets a result set from a prepared statement
while($data = $result->fetch_assoc()) {
// do stuff with first set of data
$stmt2 = $con->prepare("SELECT * from table2 WHERE id = ?"); // second prepared statement
$stmt2->bind_param('s', $data["id"]);
$stmt2->execute(); // execute second statement
$result2 = $stmt2->get_result();
while($data2 = $result2->fetch_assoc()) {
// do stuff with second set of data
}
}
我在 $noofspellschamps->bind_param('ss', $Patch_No, $champion 上收到错误 'Call to a member function bind_param() ';我已经测试了一些东西,这不是 Prepare 的问题,因为它在 phpmyadmin 上运行良好,我认为问题出在执行上,或者我可能需要先关闭来自 $champions 的流,但我需要它们,正如您在循环 我不太确定该怎么做。
EDIT2 是否有人知道问题所在并可以提供帮助?
$i=1;
$champion = array();
$noofspellschamp = array();
$Patch_No = trim($_GET['Patch_No']);
$champions = $conn->prepare("SELECT champion FROM champions where Patch_No = ?");
$champions->bind_param('s', $Patch_No);
$champions->execute();
//$champions->close();
$champions->bind_result($champ);
while($champions->fetch()){
$champion[$i]=$champ;
$noofspellschamps =$conn->prepare(
"SELECT Passive, Q, W, E, R,
((Passive != '') + (Q != '') + (W != '') + (E != '') + (R != '')
) as NumNotNull
FROM champions
WHERE Patch_No = ? AND Champion = ?");
$noofspellschamps->bind_param('ss', $Patch_No, $champion);
$noofspellschamps->execute();
$noofspellschamps->bind_result($noofspellsch);
while($noofspellschamps->fetch()){
$noofspellschamp[$i] = $noofspellsch['NumNotNull'];
echo $noofspellschamp[$i];
}
$i+=1;
}
编辑 2
你得到 "Commands out of sync error" 的原因是因为 mysql 客户端不允许你在仍然有行从以前的 in-进度查询。 Commands out of sync
使用提供的示例。 (可能需要根据自己的喜好进行更改)
$i=1;
$champion = array();
$noofspellschamp = array();
$Patch_No = trim($_GET['Patch_No']);
// query 1
$champions = $conn->prepare("SELECT champion FROM champions where Patch_No = ?");
$champions->bind_param('s', $Patch_No);
$champions->execute();
$result = $champions->get_result();
while($data = $result->fetch_assoc()){
// use data variable as an associative array
//query 2
$noofspellschamps = $conn->prepare(
"SELECT Passive, Q, W, E, R,
((Passive != '') + (Q != '') + (W != '') + (E != '') + (R != '')
) as NumNotNull
FROM champions
WHERE Patch_No = ? AND Champion = ?");
$noofspellschamps->bind_param('ss', $Patch_No, $data["champion"]);
$noofspellschamps->execute();
$result2 = $champions->get_result();
while($data2 = $result2->fetch_assoc()){
// other stuff with data2 variable
}
$i++;
}
As of PHP 5.3 mysqli_stmt::get_result
其中 returns 一个结果集对象。您可以使用 mysqli_result::fetch_array()
或 mysqli_result::fetch_assoc()
。但是,请注意,这仅适用于本机 MySQL 驱动程序。
本质上:
$stmt = $con->prepare("SELECT id FROM table1"); // first prepared statement
$stmt->execute();
$result = $stmt->get_result(); // Gets a result set from a prepared statement
while($data = $result->fetch_assoc()) {
// do stuff with first set of data
$stmt2 = $con->prepare("SELECT * from table2 WHERE id = ?"); // second prepared statement
$stmt2->bind_param('s', $data["id"]);
$stmt2->execute(); // execute second statement
$result2 = $stmt2->get_result();
while($data2 = $result2->fetch_assoc()) {
// do stuff with second set of data
}
}