准备失败:(2014)命令不同步;你现在不能运行这个命令

时间:2014-02-10 20:44:46

标签: php mysqli prepared-statement

我在这里找到了一些答案,但似乎都没有。根据我的理解,第一个数据库调用正在搞乱第二个。但我认为通过使用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">

1 个答案:

答案 0 :(得分: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();
}

只需循环浏览结果,但实际上并没有对它们做任何事情。