我遇到了“命令不同步”错误,该错误通常可以通过释放上一个查询的结果来解决,但是我不确定如何将其应用于我的方案。
我试图从php脚本各个位置的其他查询中释放结果,但该错误无法解决。我发现的示例的查询格式为$ result = $ mysqli-> query(),然后释放$ result。我没有使用这种风格的结果,但是如果我愿意,我愿意修改自己的方法。
//Check if active checklist table exists and if not create it
$activeExist = $mysqli->query("CALL TEST('active')");
while ($row = $activeExist->fetch_row()) {
echo $row[0];
error_log($row[0]."\n\n", 3, "/var/tmp/my-errors.log");
if ($row[0] == "2") {
//$activeExist->free();
$sql = "CREATE TABLE IF NOT EXISTS active (
id varchar(10) NOT NULL PRIMARY KEY,
title varchar(100)) SELECT '$activeID' AS 'id', '$realname' AS 'title';";
$mysqli->query($sql);
error_log("Active then statement from dbTableGen ".$sql."\n\n", 3, "/var/tmp/my-errors.log");
error_log("Active (" . $mysqli->errno . ") " . $mysqli->error ."\n\n", 3, "/var/tmp/my-errors.log");
//$mysqli->query("INSERT INTO active VALUES ('$activeID', '$realname')");
}
else {
error_log("Active else statement from dbTableGen"."\n\n", 3, "/var/tmp/my-errors.log");
$mysqli->query("INSERT INTO active VALUES ('$activeID', '$realname')");
}
}
}
//mysqli_free_result();
mysqli_close($mysqli);
预期结果:创建一个名为“活动”的表,如果该表不存在则插入值,或者如果该表已存在则插入值。
实际结果:命令不同步;您现在不能运行此命令
答案 0 :(得分:0)
我担心安全性/稳定性,因为您直接将变量写入迭代查询中,但是通常只缓存CALLs结果,即可触发单个查询。
此外,您应始终努力最大程度地减少访问数据库的总次数。我建议仅执行一个INSERT查询,而不要执行多个查询。
$activeExist = $mysqli->query("CALL TEST('active')");
$call_results = $activeExist->fetch_all(); // or loop if you don't have this driver installed
foreach ($call_results as $row) {
echo $row[0];
error_log($row[0] ."\n\n", 3, "/var/tmp/my-errors.log");
if ($row[0] == "2") {
//$activeExist->free();
$sql = "CREATE TABLE IF NOT EXISTS active (
id varchar(10) NOT NULL PRIMARY KEY,
title varchar(100)) SELECT '$activeID' AS 'id', '$realname' AS 'title';";
$mysqli->query($sql);
error_log("Active then statement from dbTableGen " . $sql . "\n\n", 3, "/var/tmp/my-errors.log");
error_log("Active (" . $mysqli->errno . ") " . $mysqli->error ."\n\n", 3, "/var/tmp/my-errors.log");
//$mysqli->query("INSERT INTO active VALUES ('$activeID', '$realname')");
} else {
error_log("Active else statement from dbTableGen" . "\n\n", 3, "/var/tmp/my-errors.log");
$mysqli->query("INSERT INTO active VALUES ('$activeID', '$realname')");
}
}