bind_result
vsget_result
を使用して呼び出す方法の例と、一方を他方よりも使用する目的は何かを見てみたいと思います。
また、それぞれの使用の長所と短所。
どちらかを使用することの制限は何ですか。違いはありますか。
bind_result
vsget_result
を使用して呼び出す方法の例と、一方を他方よりも使用する目的は何かを見てみたいと思います。
また、それぞれの使用の長所と短所。
どちらかを使用することの制限は何ですか。違いはありますか。
どちらの方法も*
クエリで機能しbind_result()
ますが、 を使用すると、通常は列が明示的にクエリにリストされます。bind_result()
そのため、変数の順序は返される行の構造と厳密に一致する必要があるため、 で戻り値を割り当てるときにリストを参照できます。
$query1
使用例1bind_result()
$query1 = 'SELECT id, first_name, last_name, username FROM `table` WHERE id = ?';
$id = 5;
$stmt = $mysqli->prepare($query1);
/*
Binds variables to prepared statement
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
*/
$stmt->bind_param('i',$id);
/* execute query */
$stmt->execute();
/* Store the result (to get properties) */
$stmt->store_result();
/* Get the number of rows */
$num_of_rows = $stmt->num_rows;
/* Bind the result to variables */
$stmt->bind_result($id, $first_name, $last_name, $username);
while ($stmt->fetch()) {
echo 'ID: '.$id.'<br>';
echo 'First Name: '.$first_name.'<br>';
echo 'Last Name: '.$last_name.'<br>';
echo 'Username: '.$username.'<br><br>';
}
$query2
使用例2get_result()
$query2 = 'SELECT * FROM `table` WHERE id = ?';
$id = 5;
$stmt = $mysqli->prepare($query2);
/*
Binds variables to prepared statement
i corresponding variable has type integer
d corresponding variable has type double
s corresponding variable has type string
b corresponding variable is a blob and will be sent in packets
*/
$stmt->bind_param('i',$id);
/* execute query */
$stmt->execute();
/* Get the result */
$result = $stmt->get_result();
/* Get the number of rows */
$num_of_rows = $result->num_rows;
while ($row = $result->fetch_assoc()) {
echo 'ID: '.$row['id'].'<br>';
echo 'First Name: '.$row['first_name'].'<br>';
echo 'Last Name: '.$row['last_name'].'<br>';
echo 'Username: '.$row['username'].'<br><br>';
}
長所:
短所:
長所:
fetch_all()
メソッドが返されたすべての行を一度に返すことを許可します短所:
例は、それぞれのマニュアル ページおよび にget_result()
ありbind_result()
ます。
長所と短所は非常に単純ですが、
get_result()
結果を処理する唯一の正気の方法です最新の Web アプリケーションでは、データがクエリからすぐに表示されることはありません。最初にデータを収集してから、出力を開始する必要があります。または、ベスト プラクティスに従わなくても、すぐに印刷せずにデータを返さなければならない場合があります。
それを念頭に置いて、両方の方法を使用して、選択したデータを連想配列のネストされた配列として返すコードを作成する方法を見てみましょう。
bind_result()
$query1 = 'SELECT id, first_name, last_name, username FROM `table` WHERE id = ?';
$stmt = $mysqli->prepare($query1);
$stmt->bind_param('s',$id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($id, $first_name, $last_name, $username);
$rows = [];
while ($stmt->fetch()) {
$rows[] = [
'id' => $id,
'first_name' => $first_name,
'last_name' => $last_name,
'username' => $username,
];
}
テーブルに列が追加または削除されるたびに、このコードを編集することを忘れないでください。
get_result()
$query2 = 'SELECT * FROM `table` WHERE id = ?';
$stmt = $mysqli->prepare($query2);
$stmt->bind_param('s', $id);
$stmt->execute();
$rows = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
このコードは、テーブル構造が変更されても同じままです。
そして、もっとあります。
準備/バインディング/実行の退屈なルーチンを、このように呼び出されるきちんとした関数に自動化することにした場合
$query = 'SELECT * FROM `table` WHERE id = ?';
$rows = prepared_select($query, [$id])->fetch_all(MYSQLI_ASSOC);
ほんの数行の問題でget_result()
、非常にもっともらしいタスクになります。しかし、それでは退屈なクエストになります。bind_param()
そのため、私はこのbind_result()
方法を「醜い」と呼んでいます。