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