我想看一个如何使用bind_result
与get_result
进行调用的示例,以及使用one over另一个的目的是什么。
也是使用每个的利弊。
使用任何一个的限制是什么,是否存在差异。
对我而言,决定性的因素是我是否使用*
调用我的查询列。
bind_result()
会更好:// Use bind_result() with fetch()
$query1 = 'SELECT id, first_name, last_name, username FROM table WHERE id = ?';
get_result()
会更好:// Use get_result() with fetch_assoc()
$query2 = 'SELECT * FROM table WHERE id = ?';
$query1
的bind_result()
的示例1$query1 = 'SELECT id, first_name, last_name, username FROM table WHERE id = ?';
$id = 5;
if($stmt = $mysqli->prepare($query)){
/*
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>';
}
/* free results */
$stmt->free_result();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
$query2
的get_result()
的示例2$query2 = 'SELECT * FROM table WHERE id = ?';
$id = 5;
if($stmt = $mysqli->prepare($query)){
/*
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>';
}
/* free results */
$stmt->free_result();
/* close statement */
$stmt->close();
}
/* close connection */
$mysqli->close();
如你所见,你不能使用bind_result
与*
。然而,get_result
适用于两者,但bind_result
更简单,并拿出一些混乱与$row['name']
。
优点:
$row['name']
fetch()
缺点:
*
的SQL查询优点:
fetch_assoc()
缺点:
$row[]
您可以在相应的手册页上找到示例。
虽然赞成和缺点很简单:
无论如何,如果你的想法是在应用程序代码中使用任何一个函数 - 这个想法是错误的。然而,只要你将它们封装在一些方法中以从查询中返回数据,那么使用哪一个并不重要,除了你需要十倍的代码来实现bind_result这一事实。
我注意到的主要区别是bind_result()
给你错误2014
,当你试图在其他$ stmt中编译嵌套的$ stmt时,正在获取(没有mysqli::store_result()
):
准备失败:(2014)命令不同步;你现在不能运行这个命令
function GetUserName($id)
{
global $conn;
$sql = "SELECT name FROM users WHERE id = ?";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($name);
while ($stmt->fetch()) {
return $name;
}
$stmt->close();
} else {
echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
}
}
$sql = "SELECT from_id, to_id, content
FROM `direct_message`
WHERE `to_id` = ?";
if ($stmt = $conn->prepare($sql)) {
$stmt->bind_param('i', $myID);
/* execute statement */
$stmt->execute();
/* bind result variables */
$stmt->bind_result($from, $to, $text);
/* fetch values */
while ($stmt->fetch()) {
echo "<li>";
echo "<p>Message from: ".GetUserName($from)."</p>";
echo "<p>Message content: ".$text."</p>";
echo "</li>";
}
/* close statement */
$stmt->close();
} else {
echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
}
我认为示例2只会像这样工作,因为store_result和get_result都从表中获取信息。
所以删除
/* Store the result (to get properties) */
$stmt->store_result();
并稍微改变顺序。这是最终结果:
$query2 = 'SELECT * FROM table WHERE id = ?';
$id = 5;
if($stmt = $mysqli->prepare($query)){
/*
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>';
}
/* free results */
$stmt->free_result();
get_result()现在只能通过安装MySQL本机驱动程序(mysqlnd)在PHP中使用。在某些环境中,可能无法或不希望安装mysqlnd。
尽管如此,您仍然可以使用mysqli来执行'select *'查询,并使用字段名称获取结果 - 尽管它比使用get_result()稍微复杂一些,并涉及使用php的call_user_func_array()函数。请参阅How to use bind_result() instead of get_result() in php上的示例,该示例执行简单的“select *”查询,并将结果(带有列名称)输出到HTML表。