【问题标题】:PHP - Getting: Commands out of sync; you can't run this command nowPHP - 获取:命令不同步;你现在不能运行这个命令
【发布时间】:2016-04-11 17:12:08
【问题描述】:

我知道有数百个类似的问题,我已经尝试了所有方法,但没有一个对我真正有用。

我的 MariaDB 中有一个调用存储过程的函数。这是返回数组。

<?

class MyClass {

protected static $connection;

    public function connect() {    
        // Try and connect to the database
        if(!isset(self::$connection)) {
            self::$connection = new mysqli(SERVERNAME,USERNAME,PASS,DBNAME);
        }
        // If connection was not successful, handle the error
        if(self::$connection === false) {
            // Handle error - notify administrator, log to a file, show an error screen, etc.
            return false;
        }
        return self::$connection;
    }

    public function query($query) {
        // Connect to the database
        $connection = $this -> connect();

        // Query the database
        $result = $connection -> query($query);

        return $result;
    }

    public function quote($value) {
        $connection = $this -> connect();
        return $connection -> real_escape_string($value);
    }

public function CallStoredProc($query) {
    // Connect to the database
    $connection = $this -> connect();

    // Query the database
    $result = $connection -> query($query,MYSQLI_USE_RESULT);   //,

    if($result === false) {
        return false;
    }

    while ($row = $result -> fetch_assoc()) {
        $rows[] = $row;
    }

    $result->free();
    return $rows;
    }

function StoreProcessed($Name,$Price){
    //escape
    $Name = $this->quote($Name);
    $Price= $this->quote($Price);

    $SQL = "INSERT INTO Result (`Name`,`Price`) VALUES ('$Name','$Price');";
    $result = $this->query($SQL);

    }


    //the function I am using for processing: 
function Compare($ID) {

    $query = "CALL MyProcedure($ID);";
    $result =$this->CallStoredProc($query);

    /*After the array is returned I am looping trough each element of array 
    and storing this in DB with another function. */

    $Table = "<table>";
    foreach ($result as $key=>$val)
        {
            $Name   =   $result[$key]["Name"];  
            $Price  =   $result[$key]["Price"];
            $this->StoreProcessed($Name,$Price);

            //This is where the Commands out of sync is returned

            $Table = $Table. "<tr>
                        <td>$Name</td>
                        <td>$Price</td>
                            </tr>";
                }
            $Table = $Table. "</table>";
        return $Table;
    }

    }

我的 php 文件如下所示:

<?
$auto = new MyClass();
$table = $auto->Compare(14);

echo $table;
?>

我正在使用MYSQLI_USE_RESULT,数组填充后,我也在使用mysqli_free_result。我还应该做什么?

非常感谢!

【问题讨论】:

标签: php mysql arrays mysqli


【解决方案1】:

我找到了答案 here 。刚刚添加了这个功能:

function free_all_results(mysqli $dbCon)
{
    do {
        if ($res = $dbCon->store_result()) {
            $res->fetch_all(MYSQLI_ASSOC);
            $res->free();
        }
    } while ($dbCon->more_results() && $dbCon->next_result());
}

我在返回结果之前调用了它:

public function CallStoredProc($query) {
// Connect to the database
    $connection = $this -> connect();

    // Query the database
    $result = $connection -> query($query); //,
    mysqli_store_result($connection);

    if($result === false) {
        return false;
    }

    while ($row = $result -> fetch_assoc()) {
        $rows[] = $row;
    }

    $this->free_all_results($connection);
    return $rows;
}

【讨论】:

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