如何用准备好的语句获取mysqli_result?

时间:2013-02-20 19:08:35

标签: php

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

bind_result into an array PHP mysqli prepared statement

它回答了同样的问题。我不知道如何删除我自己的帖子,现在我认为这是以前问过的问题的副本。主持人,请随时删除这篇文章。谢谢。


我正在将我的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>";
    }
}

1 个答案:

答案 0 :(得分: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