1

当我在寻找另一个问题的答案时,我遇到了这个线程:

bind_result 成数组 PHP mysqli 准备好的语句

它回答了同样的问题。我不知道如何删除我自己的帖子,现在我将其视为与之前提出的问题的重复。版主,请随意删除此帖。谢谢。


我正在将我的 php 脚本转换为准备好的语句,但是我无法找出正确的语法来获取关联数组。以前构建的非准备版本可以工作,但我不知道如何检索 mysqli_result。我尝试了各种我认为可能有效的突变(其中一些在下面提到),但到目前为止没有一个有效。我要么得到“给定的对象”,要么得到“给定的布尔值”。我究竟做错了什么?如何检索 mysqli_result?

这是有效的 php 脚本的非准备版本(简化以演示工作版本与非工作准备状态版本之间的区别):

// THIS WORKS
$dbh = mysqli_connect('localhost', $db_user, $db_password);
mysqli_select_db($dbh, $db_dbname);

$num = 123456; 
$num0 = 0; 
$num1 = 1; 
$i = 0; 
$myList = array(array()); 

$query = "SELECT col1, col2, col3, col4, col5, colB, colC 
    FROM tableOne LEFT OUTER JOIN tableTwo ON( tableOne.col3=tableTwo.colA )
    WHERE col6='" . $num . "' AND col7='" . $num1 . "'
    AND col8='" . $num1 . "' AND col9='" . $num0 . "'";

$data = mysqli_query($dbh, $query);

if ($data) { $dataCount = mysqli_num_rows($data); }

if ($dataCount > 0) {
    while( $row = mysqli_fetch_assoc($data) ) {
        $thisCol1 = $row['col1']; 
        $thisCol2 = $row['col2'];
        $thisCol3 = $row['col3']; 
        $thisCol4 = $row['col4'];
        $thisCol5 = $row['col5'];
        if ($thisCol1 > $thisCol2) {
            $myList[$i]["result1"] = "good"; 
        } else { 
            $myList[$i]["result1"] = "bad";
        }
        if ($thisCol3 > $thisCol4) { 
            $myList[$i]["result2"] = "good"; 
        } else { 
            $myList[$i]["result2"] = "bad"; 
        }
        $myList[$i]["thisCol5"] = $row['col5'];
        $myList[$i]["thisColB"] = $row['colB'];
        $myList[$i]["thisColC"] = $row['colC'];
        $i = $i + 1;
    }
    echo json_encode($myList);
} else {
    echo "no entry found";
}

这是转换为给我错误的准备好的语句的尝试:

// THIS GIVES ME ERROR
$mysqli = new mysqli('localhost', $db_user, $db_password, $db_dbname);
$stmt = $mysqli->stmt_init();

$num = 123456; 
$num0 = 0; 
$num1 = 1; 
$i = 0; 
$myList = array(array()); 

$query = "SELECT col1, col2, col3, col4, col5, colB, colC 
    FROM tableOne LEFT OUTER JOIN tableTwo ON( tableOne.col3=tableTwo.colA )
    WHERE col6=? AND col7=? AND col8=? AND col9=?";

if($stmt->prepare($query)){

    $stmt->bind_param("iiii", $num, $num1, $num1, $num0);

    $stmt->execute() or trigger_error($mysqli->error);

    if ($stmt->store_result()) {

        $stmt->bind_result($col1,$col2,$col3,$col4,$col5,$colB,$colC);

        while mysqli_fetch_assoc($stmt) {
            $thisCol1 = $col1;  
            $thisCol2 = $col2;
            $thisCol3 = $col3;  
            $thisCol4 = $col4; 
            $thisCol5 = $col5;
            if ($thisCol1 > $thisCol2) { 
                $myList[$i]["result1"] = "good"; 
            } else { 
                $myList[$i]["result1"] = "bad";
            }
            if ($thisCol3 > $thisCol4) { 
                $myList[$i]["result2"] = "good"; 
            } else { 
                $myList[$i]["result2"] = "bad"; 
            }
            $myList[$i]["thisCol5"] = $col5;
            $myList[$i]["thisColB"] = $colB;
            $myList[$i]["thisColC"] = $colC;
            $i = $i + 1;
        }
        echo json_encode($myList);
    }
} else {
    echo $mysqli->error;
    echo "no entry found";
    $mysqli->close();  // close connection
}

我尝试了 $rows=$stmt->execute() 和 mysqli_fetch_assoc($rows),但没有成功。我也尝试了 $rows=$stmt->bind_result($col1,$col2,$col3,$col4,$col5,$colB,$colC),但它也不起作用。然后我尝试了 $rows=$stmt->fetch(),还是不行。

我找不到从已执行语句中检索 mysqli_result 的正确语法。我非常感谢任何和所有的帮助。

已解决: 感谢 PeeHaa,我学会了如何使用准备好的语句来做到这一点。这是让 $myList 返回我正在寻找的数组的第一步。

// REPLACE THE if($stmt->prepare($query)){} SEGMENT WITH THE FOLLOWING 
// AND THE SOLUTION WILL BE STARING AT YOU:

if($stmt->prepare($query)){
    $stmt->bind_param("iiii", $num, $num1, $num1, $num0);
    $stmt->execute() or trigger_error($mysqli->error);
    $rows = $stmt->get_result();
    while ($row=mysqli_fetch_array($rows)){
        echo "<p>" . $row['col1'] . "</p>";
    }
}
4

1 回答 1

0

根据http://www.php.net/manual/en/mysqli-result.fetch-assoc.php fetch_assoc() 接受 void 参数或 mysqli_result。但是您在 mysqli_statement 中发送:

while mysqli_fetch_assoc($stmt) {

编辑:我看到你将结果绑定到变量。如果要填充变量,请改用此循环:

while ($stmt->fetch()) {

( http://www.php.net/manual/en/mysqli-stmt.fetch.php )

于 2013-02-20T19:53:18.197 回答