【问题标题】:PHP mysqli Commands out of sync; you can't run this command nowPHP mysqli 命令不同步;你现在不能运行这个命令
【发布时间】:2013-10-05 08:18:01
【问题描述】:

在我的数据库中,我有一些存储过程。

我想调用其中一个,根据我从这个过程中得到的结果,我将确定天气是否调用第二个。

正如您在isUserValid() 方法中看到的,我使用了$result->free_results()

但在调用其他程序时我仍然得到:Commands out of sync; you can't run this command now

这是我对 db 函数的调用:

    /**
     * Run a new query on the db.
     * this will open an close the connection to the db
     * @param $query string the query to run
     * @param $verifyAccessToken boolean true if user validation is needed before query is executed, false otherwise
     * @return bool|mysqli_result the result of the query if any or true or false in a non query
     * @throws Exception on connection and query errors
     */
    private function queryDb($query,$verifyAccessToken)
    {
       // if ($this->test)
//            echo $query . "<br>";
        //create a new mysqli connection
        $mysqli = new mysqli($this->DB_HOST, $this->DB_USER, $this->DB_PASS, $this->DB_NAME);
        //set the charset for the connection
        if (!$mysqli->set_charset("utf8"))
        {
            $mysqli->close();
            throw new Exception ("Error setting UTF 8 DB connection");
        }
        //check if the connection was ok
        if (mysqli_connect_errno())
        {
            $mysqli->close();
            throw new Exception("Error connecting DB");
        }

        //if verification is needed - verify access token
       if ($verifyAccessToken && !$this->isUserValid($mysqli))
       {
          $mysqli->close();
          throw new Exception(ACCESS_TOKEN_INCORRECT);
       }

        //get results
        $result = $mysqli->query($query);
        //check if there were now error in query. if so - throw an exception
        if (!$result)
        {
            $mysqlError = $mysqli->error . __LINE__;
            //close the connection
            $mysqli->close();
            throw new Exception ("mySQL Error: " . $mysqlError);
        }
        //fetch the results into an array
        $rows = $result->fetch_all(MYSQLI_BOTH);
        //close the connection
        $result->close();
        $result->free();
        $mysqli->close();
        //return the results if any
        return $rows;
    }

    /**
     * Check if user is valid
     * @param $mysqli mysqli connection to db
     * @return true if user is valid, false otherwise
     */
    function isUserValid($mysqli)
    {
        //Check if there is a saved access token. if not - throw an exception
        if (is_null($this->user) || $this->user->getPicoAccessToken() == null)
          throw new Exception(ACCESS_TOKEN_INCORRECT);
        //get user details
        $facebookId = $this->user->getFacebookId();
        $picoAccessToken = $this->user->getPicoAccessToken();
        //create verification query
        $verificationQuery = "Call isUserValid('$facebookId','$picoAccessToken')";
        //execute query
        $result = $mysqli->query($verificationQuery);

        //if query had error - throw exception
        if (!$result)
        {
         $mysqlError = $mysqli->error . __LINE__;
         //close the connection
         $mysqli->close();
         throw new Exception ("mySQL Error: " . $mysqlError);
        }
        $rows = $result->fetch_all(MYSQLI_ASSOC);
        $rows = $rows[0];
        $result->free_result();
        //return user valid status
        return isset($rows["isUserValid"]) && $rows["isUserValid"] == 1;
    }

【问题讨论】:

    标签: php mysql mysqli


    【解决方案1】:

    每个存储过程至少返回两个结果

    • 一个(或多个)实际结果
    • 一个空结果告诉客户端没有更多结果。

    你必须使用mysqli_more_results()/mysqli_next_result() 来清理它们。

    如果您的过程只返回一个结果,或者您只需要第一个结果之外的任何额外结果,您可以使用以下代码 sn-p 清理结果队列:

    while($mysqli->more_results())
    {
        $mysqli->next_result();
        if($res = $mysqli->store_result())
        {
            $res->free(); 
        }
    }
    

    【讨论】:

    • 这真的很有帮助,解决了我的问题。仅供参考:没有 $mysqli->free_result() 方法..它在 $res 对象下..
    • 谢谢,我的错。固定
    • 哇。这次真是万分感谢。我没有在其他任何地方看到过第二个结果集。
    【解决方案2】:

    当前接受的答案不会按原样工作,我认为不应在第一个结果之前调用 next_result()

    这是一个有效的例子:

    if (!$mysqli->multi_query("CALL p()")) {
        echo "CALL failed: (" . $mysqli->errno . ") " . $mysqli->error;
    }
    
    do {
        if ($res = $mysqli->store_result()) {
            printf("---\n");
            var_dump($res->fetch_all());
            $res->free();
        } else {
            if ($mysqli->errno) {
                echo "Store failed: (" . $mysqli->errno . ") " . $mysqli->error;
            }
        }
    } while ($mysqli->more_results() && $mysqli->next_result());
    

    来自 PHP 手册http://php.net/manual/en/mysqli.quickstart.stored-procedures.php

    【讨论】:

      【解决方案3】:

      重新连接数据库$this-&gt;db-&gt;reconnect(); // likes in codeigniter

      【讨论】:

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