我想看一个例子,说明如何使用bind_result
和get_result
来调用,以及使用其中一个来替代另一个的目的是什么.
还有使用每种方法的利弊.
使用这两种方法的限制是什么,有什么不同吗?
我想看一个例子,说明如何使用bind_result
和get_result
来调用,以及使用其中一个来替代另一个的目的是什么.
还有使用每种方法的利弊.
使用这两种方法的限制是什么,有什么不同吗?
尽管这两种方法都适用于*
个查询,但当使用bind_result()
时,列通常会在查询中显式列出,因此在bind_result()
中分配返回值时可以参考列表,因为变量的顺序必须严格匹配返回行的 struct .
$query1
using bind_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
using get_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>';
}
Pros:
Cons:
Pros:
fetch_all()
方法一次返回所有返回的行Cons: