Mysqli返回错误"命令不同步"当试图做多个准备好的陈述时

时间:2015-01-03 16:19:18

标签: php sql arrays mysqli prepared-statement

我收到错误'调用成员函数bind_param()' on $ noofspellschamps-> bind_param(' ss',$ Patch_No,$ champion);我已经测试了很少的东西而且它不是Prepare的问题,因为它在phpmyadmin上正常工作,我认为问题在于执行或者我可能需要首先从$ champions关闭流但我需要它们两个正如你在循环中看到的那样,我不确定该怎么做。

EDIT2 是否有人知道问题所在并可能有所帮助?

 $i=1;
                $champion = array();
                $noofspellschamp = array();

                $Patch_No = trim($_GET['Patch_No']);

                $champions = $conn->prepare("SELECT champion FROM champions where Patch_No = ?");
                $champions->bind_param('s', $Patch_No);
                $champions->execute();
                //$champions->close();

                $champions->bind_result($champ);



                    while($champions->fetch()){

                        $champion[$i]=$champ;    

                        $noofspellschamps =$conn->prepare(
                        "SELECT Passive, Q, W, E, R,
                                ((Passive != '') + (Q != '') + (W != '') + (E != '') + (R != '')
                                ) as NumNotNull
                        FROM champions
                        WHERE Patch_No = ? AND Champion = ?");
                        $noofspellschamps->bind_param('ss', $Patch_No, $champion);
                        $noofspellschamps->execute();
                        $noofspellschamps->bind_result($noofspellsch);



                        while($noofspellschamps->fetch()){
                            $noofspellschamp[$i] = $noofspellsch['NumNotNull'];
                            echo $noofspellschamp[$i];
                        }
                        $i+=1;
                    }

1 个答案:

答案 0 :(得分:1)

编辑2

您获取"命令不同步错误的原因"是因为当从先前正在进行的查询中仍然获取行时,mysql客户端不允许您进行新的查询/子查询。 Commands out of sync

使用提供的示例。 (可能必须根据自己的喜好进行更改)

$i=1;
$champion = array();
$noofspellschamp = array();

$Patch_No = trim($_GET['Patch_No']);

// query 1
$champions = $conn->prepare("SELECT champion FROM champions where Patch_No = ?");
$champions->bind_param('s', $Patch_No);
$champions->execute();
$result = $champions->get_result();


while($data = $result->fetch_assoc()){

    // use data variable as an associative array

    //query 2
    $noofspellschamps = $conn->prepare(
    "SELECT Passive, Q, W, E, R,
            ((Passive != '') + (Q != '') + (W != '') + (E != '') + (R != '')
            ) as NumNotNull
    FROM champions
    WHERE Patch_No = ? AND Champion = ?");

    $noofspellschamps->bind_param('ss', $Patch_No, $data["champion"]);
    $noofspellschamps->execute();
    $result2 = $champions->get_result();



    while($data2 = $result2->fetch_assoc()){
        // other stuff with data2 variable
    }

    $i++;
}

从PHP 5.3 mysqli_stmt::get_result开始,它返回一个结果集对象。您可以使用mysqli_result::fetch_array()mysqli_result::fetch_assoc()。但请注意,这仅适用于本机MySQL驱动程序。

本质上:

$stmt = $con->prepare("SELECT id FROM table1"); // first prepared statement
$stmt->execute(); 
$result = $stmt->get_result(); // Gets a result set from a prepared statement
while($data = $result->fetch_assoc()) {  
    // do stuff with first set of data

    $stmt2 = $con->prepare("SELECT * from table2 WHERE id = ?"); // second prepared statement
    $stmt2->bind_param('s', $data["id"]);
    $stmt2->execute(); // execute second statement
    $result2 = $stmt2->get_result();
    while($data2 = $result2->fetch_assoc()) { 

        // do stuff with second set of data
    }

}