【问题标题】:Prepare failed: (2014) Commands out of sync; you can't run this command now准备失败:(2014)命令不同步;你现在不能运行这个命令
【发布时间】:2014-02-10 20:44:45
【问题描述】:

我在这里找到了一些答案,但它们似乎都不起作用。据我了解,第一个数据库调用与第二个数据库调用混淆了。但我认为通过使用 close() 和 unset() 应该关闭第一个调用...

有什么想法吗?我需要第一个查询的结果,然后只需要第二个查询的行数......

//See if there are any sales that need printing
if ($stmt = $mysqli->prepare("SELECT quantity FROM sales WHERE printed = 0 AND DATE_FORMAT(saledatetime, '%Y/%m/%d') = ? AND venue = ?")) {
    $stmt->bind_param('ss',$_SESSION['session_date'],str_replace(" ","",strtolower($_SESSION['venue'])));
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($qty);

    $new = 0;
    while($stmt->fetch()) {
        $new = ($new + $qty);
    }
    //Close connection
    $stmt->close();
    unset($stmt);
}

//See if an upload has been done
if ($stmt = $mysqli->prepare("SELECT id FROM images WHERE udate = ? AND venue = ?")) {
    $stmt->bind_param('ss',$_SESSION['session_date'],str_replace(" ","",strtolower($_SESSION['venue'])));
    $stmt->execute();
    $stmt->bind_result($imgs_tmp);
    $rows = $stmt->num_rows;
}

编辑: 完整的标题代码

<?php
session_start();
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

//Make additional session keys
$_SESSION['ROOT_DIR'] = "http://".$_SERVER['HTTP_HOST']."/ppa/";
$_SESSION['ROOT_PATH'] = $_SERVER['DOCUMENT_ROOT']."/ppa/";

include "includes/db_connect.php";
include "includes/functions.php";
include "includes/required.php";

//See if there are any sales that need printing
if ($stmt = $mysqli->prepare("SELECT quantity FROM sales WHERE printed = 0 AND DATE_FORMAT(saledatetime, '%Y/%m/%d') = ? AND venue = ?")) {
    $stmt->bind_param('ss',$_SESSION['session_date'],str_replace(" ","",strtolower($_SESSION['venue'])));
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($qty);

    $new = 0;
    while($stmt->fetch()) {
        $new = ($new + $qty);
    }
    //Close connection
    $stmt->free_result();
    $stmt->close();
}

//See if an upload has been done
if ($stmt = $mysqli->prepare("SELECT id FROM images WHERE udate = ? AND venue = ?")) {
    $stmt->bind_param('ss',$_SESSION['session_date'],str_replace(" ","",strtolower($_SESSION['venue'])));
    $stmt->execute();
    $stmt->bind_result($imgs_tmp);
    $rows = $stmt->num_rows;

    $stmt->free_result();
    $stmt->close();
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

【问题讨论】:

  • 你试过在$stmt-&gt;close();之前运行$stmt-&gt;free_result();吗?
  • 是的,如果我对第二个查询也这样做,错误不会显示,但是当应该有时我没有返回任何行:S
  • 这是整个脚本吗?第二个查询没有$stmt-&gt;close();,但应该有。
  • 是的@EdCottrell,请参阅更新后的完整标题,该标题在文档类型和标题标签等的开头结束...
  • 查看更新后的帖子@Diamondo25

标签: php mysqli prepared-statement


【解决方案1】:

搞定了。

//See if an upload has been done
if ($stmt = $mysqli->prepare("SELECT id FROM images WHERE udate = ? AND venue = ?")) {
    $stmt->bind_param('ss',$_SESSION['session_date'],str_replace(" ","",strtolower($_SESSION['venue'])));
    $stmt->execute();
    $stmt->bind_result($m);
    while($stmt->fetch()) { $m; }
    $rows = $stmt->num_rows;
    $stmt->free_result();
    $stmt->close();
}

只是简单地循环遍历结果,但实际上并没有对它们做任何事情。

【讨论】:

    猜你喜欢
    • 2021-06-11
    • 1970-01-01
    • 2013-08-10
    • 2016-05-28
    • 2015-04-16
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    相关资源
    最近更新 更多