1

Edit:

Okay, so, I did a little more testing and found out it was stored procedures that mess everything up. But I don't know why stored procedures mess it up. See my first comment under this post for details.

/Edit

Okay, so, I've got this school project I've been working on using my laptop as a server. However, I cannot submit it without putting it on the school's servers. My lecturer finally gave me access to it and I finally was able to start migrating my project there.

After fixing a tonne of smaller issues like default charset mismatch and case sensitivity issues, this particular problem came along and has me stumped. I spent a few hours running through my code, looking through PHP documentation and mangling my code to print out walls of debug text and finally re-produced the bug with as little code as possible.

Here's the PHP code:

<?php
    define("HOST", "someHost");
    define("USER", "someUser");
    define("PASS", "somePass");
    define("DB"  , "someDB");
    $conn = mysqli_connect(HOST, USER, PASS, DB);

    $stmt1 = $conn->stmt_init();
    if (!$stmt1->prepare("CALL P_Topics_Get(33);")) {//If I uncomment this, I get an error when trying to prepare $stmt2
    //if (!$stmt1->prepare("SELECT * FROM Topics WHERE id = 33;")) { //If I uncomment this, I DON'T get an error anywhere
    //if (!$stmt1->prepare("SELECT 0, 'name', 1, 2, 3;")) {//If I uncomment this, I DON'T get an error anywhere
        echo "Failed to prepare 1: " . $conn->errno . ", " . $conn->error;
        exit;
    }
    if (!$stmt1->execute()) {
        echo "Failed to execute 1: " . $conn->errno . ", " . $conn->error;
        exit;
    }
    if (!$stmt1->store_result()) {
        echo "Failed to store 1: " . $conn->errno . ", " . $conn->error;
        exit;
    }
    $stmt1->bind_result($id, $name, $step, $decAmt, $maxAmt);
    if (!$stmt1->fetch()) {
        echo "Failed to fetch 1: " . $conn->errno . ", " . $conn->error;
        exit;
    }
    echo "$id, $name, $step, $decAmt, $maxAmt<br>";
    $stmt1->free_result();
    $stmt1->close();

    $stmt2 = $conn->stmt_init();
    if (!$stmt2->prepare("SELECT 2;")) {
        echo "Failed to prepare 2: " . $conn->errno . ", " . $conn->error;
        exit;
    }
    if (!$stmt2->execute()) {
        echo "Failed to execute 2: " . $conn->errno . ", " . $conn->error;
        exit;
    }
    if (!$stmt2->store_result()) {
        echo "Failed to store 2: " . $conn->errno . ", " . $conn->error;
        exit;
    }
    $stmt2->bind_result($val2);
    if (!$stmt2->fetch()) {
        echo "Failed to fetch 2: " . $conn->errno . ", " . $conn->error;
        exit;
    }
    echo "Val 2: " . $val2 . "<br>";
    $stmt2->free_result();
    $stmt2->close();

    echo "success";
?>

P_Topics_Get() is a simple select statement from a table:

SELECT
  *
FROM
  Topics t
WHERE
  t.id = topicId;

So.. Yeah. This works on my localhost with no errors but messes everything up on the school's machine. This is the first time I have ever encountered the 2014 error.. And it's only 2013! (/joke)

Anyone know what's up?

4

2 回答 2

1

好吧,我找到了解决方法。如果您在某处有一个 dbconfig.php 文件,请向其中添加一个简单的函数:

function reconnect () {
    global $conn;
    $conn->close();
    $conn = mysqli_connect(HOST, USER, PASS, DB);
}

我的 dbconfig.php 文件如下所示:

<?php
    //Configurables
    define("HOST", "someHost");
    define("USER", "someUser");
    define("PASS", "somePass");
    define("DB"  , "someDB");
    //End Configurables
    $conn = mysqli_connect(HOST, USER, PASS, DB);

    if (mysqli_connect_errno()) {
        echo "Server error: Please try again later.";
        //printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    function reconnect () {
        global $conn;
        $conn->close();
        $conn = mysqli_connect(HOST, USER, PASS, DB);
    }
?>

然后,每次调用存储过程后,调用reconnect()!

$stmt = $conn->stmt_init(); //Old $conn
$stmt->prepare("CALL SomeProcedure();");
$stmt->execute();
$stmt->store_result();
$stmt->bind_result();
$stmt->fetch();
$stmt->close();
reconnect(); //$conn is closed and re-opened

//$conn is a new connection

问题是,就我而言,如果您调用存储函数,则不必reconnect()。如中,SELECT F_SomeFunction();不需要一段reconnect()时间CALL P_SomeProcedure();

于 2013-08-27T03:25:11.767 回答
0

您可能需要清除更多结果,和/或关闭光标。

请参阅:Python,“命令不同步;您现在无法运行此命令”

http://php.net/manual/en/pdostatement.closecursor.php

抱歉,我不是 PHP 开发人员,所以没有费心去尝试。

于 2013-08-22T14:40:16.847 回答