私のホスティング会社はMySQLndを提供していないので、コードのいくつかを変更する必要がありました。
これが私のコードです:
<?php
include_once 'db.php';
$sql = "SELECT * FROM table1 ORDER BY id DESC";
$stmt = mysqli_stmt_init($connect);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo "SQL statement failed";
} else{
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
while ($row = mysqli_fetch_assoc($result)) {
...
私$result = mysqli_stmt_get_result($stmt);
が$result = mysqli_stmt_bind_result($stmt);
それに変更すると私に与えます
Warning: Wrong parameter count for mysqli_stmt_bind_result()
そして
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, null given
私はこれに2日間取り組んできましたが、初心者のため解決できませんでした。私は何をすべきか?
手続き型mysqliを使用してから何年も経っているので、これについては我慢してください...
列パラメータを動的にバインドしてフェッチループを実行する場合(mysqlndはオプションではないため)、ここでのこのコードはそれを実現します。
mysqli_stmt_execute($stmt);
// now before you loop on $stmt->fetch, you must bind all the columns that exist
// to a row which will hold the values during the looping on fetch (yes, confusing)
$row = array(); // will hold each fetch'd loop result
$params = array(); // something to pass keyed references to $row with
$params[] = $stmt; // add the $stmt object as first param (for procedural way)
$meta = mysqli_stmt_result_metadata($stmt);// get what those columns will be
while($field = $meta->fetch_field()) {
if (!isset($row[ $field->name ])) { $row[ $field->name ] = null; } // set if not set
$params[] = &$row[ $field->name ]; // add reference to keyed row value
}
$meta->close();// metadata no longer needed, close it
call_user_func_array('mysqli_stmt_bind_result', $params);
while (mysqli_stmt_fetch($stmt)) {
// in here you then have $row to use as before
echo $row['id'];
}
さて、あなたは私がここでやり過ぎだと思っているかもしれません...はい。この例は、(構文を使用して)不明なSQL列グラブを処理するためのものSELECT *
です。ただし、すべての列が出力されることがわかっている場合は、次のようにwhileループの前に各列をバインドするだけです。
mysqli_stmt_execute($stmt);
mysqli_stmt_bind_result($stmt, $id, $col2, $col3);
while (mysqli_stmt_fetch($stmt)) {
// in here you then use $id, $col2, $col3
echo $id;
}
これが、PDO
ライブラリに切り替えると、物事がひどく簡単になる理由です。ライブラリには、ご存知のように、mysqlnd
利用可能なものが大好きな単純なループで、関連付けられたキー名を含む行を単純にフェッチするためのプロビジョニングがあります。ただし、それPDO
も選択肢にない場合は、「バインド」と「mysqli」を処理するための苦痛で難解な方法が残されています。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加