Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
585 views
in Technique[技术] by (71.8m points)

mysql - PHP mysqli Commands out of sync; you can't run this command now

In my db I have some stored procedures.

I want to call the one of them, and according to the results I get from this procedure, i will determine weather to call the second one or not.

As you can see in the isUserValid() method I've used $result->free_results()

but still when calling the other procedure I get: Commands out of sync; you can't run this command now

This is my calling to the db functions:

    /**
     * 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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Every stored procedure returns at least two results

  • one (or many) actual results
  • one empty result to tell the client there are no more results.

You have to use mysqli_more_results()/mysqli_next_result() to clean them.

If your procedure returns only one result, or you just don't want any extra results but the first one, you can clean up the results queue with this code snippet:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...