Php 如何使用绑定结果与获取结果的示例
我想看一个如何使用Php 如何使用绑定结果与获取结果的示例,php,mysql,mysqli,prepared-statement,Php,Mysql,Mysqli,Prepared Statement,我想看一个如何使用bind\u result调用与get\u result调用的示例,以及使用其中一个的目的是什么 还有使用每种方法的优点和缺点 使用这两种方法的局限性是什么,有什么区别。对我来说,决定因素是我是否使用*调用查询列 使用bind\u result()会更好: 使用get\u result()会更好: 使用bind\u result() 正如您所看到的,您不能将bind\u result与*一起使用。然而,get_result对这两种方法都有效,但是bind_result更简单
bind\u result
调用与get\u result
调用的示例,以及使用其中一个的目的是什么
还有使用每种方法的优点和缺点
使用这两种方法的局限性是什么,有什么区别。对我来说,决定因素是我是否使用
*
调用查询列
使用bind\u result()
会更好:
使用get\u result()
会更好:
使用
bind\u result()
正如您所看到的,您不能将
bind\u result
与*
一起使用。然而,get_result
对这两种方法都有效,但是bind_result
更简单,并且消除了$row['name']
的一些混乱
bind_result() 优点:
- 简单的
- 无需处理
$row['name']
- 使用
fetch()
- 不适用于使用
*
获取_结果() 优点:
- 适用于所有SQL语句
- 使用
fetch\u assoc()
- 必须处理数组变量
$row[]
- 不那么整洁
- 需要MySQL本机驱动程序()
*
调用查询列
使用bind\u result()
会更好:
使用get\u result()
会更好:
使用
bind\u result()
正如您所看到的,您不能将
bind\u result
与*
一起使用。然而,get_result
对这两种方法都有效,但是bind_result
更简单,并且消除了$row['name']
的一些混乱
bind_result() 优点:
- 简单的
- 无需处理
$row['name']
- 使用
fetch()
- 不适用于使用
*
获取_结果() 优点:
- 适用于所有SQL语句
- 使用
fetch\u assoc()
- 必须处理数组变量
$row[]
- 不那么整洁
- 需要MySQL本机驱动程序()
- get_result是处理结果的唯一合理方法
- 但是它并不总是可用的,您的代码必须使用丑陋的bind_结果进行回退
无论如何,如果您的想法是在应用程序代码中正确使用任何一个函数,那么这个想法是错误的。但是,只要您将它们封装在某种方法中,以便从查询返回数据,那么使用哪种方法并不重要,只需要多编写十倍的代码来实现bind_结果 您可以在相应的手册页面上找到示例 虽然赞成和反对意见都很简单:
- get_result是处理结果的唯一合理方法
- 但是它并不总是可用的,您的代码必须使用丑陋的bind_结果进行回退
无论如何,如果您的想法是在应用程序代码中正确使用任何一个函数,那么这个想法是错误的。但是,只要您将它们封装在某种方法中,以便从查询返回数据,那么使用哪种方法并不重要,只需要多编写十倍的代码来实现bind_结果 我注意到的主要区别是,
bind_result()
在尝试将嵌套的$stmt编码到其他$stmt中时会出现错误2014
,这是获取的(没有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; }
$sql=“选择从\u id、到\u id、内容 来自“直接消息” 其中“to_id`=?”; 如果($stmt=$conn->prepare($sql)){ $stmt->bind_参数('i',$myID); /*执行语句*/ $stmt->execute(); /*绑定结果变量*/ $stmt->bind_result($from,$to,$text); /*获取值*/ 而($stmt->fetch()){ 回声“
- ”; echo“消息来源:“.GetUserName($from)。””; 回显“消息内容:“.$text.””; 回声“
”; } /*结束语*/ $stmt->close(); }否则{ echo“准备失败:(“$conn->errno.”“$conn->error; }
bind_result()
在尝试将嵌套的$stmt编码到其他$stmt中时,会出现错误2014
,这是获取的(没有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; }
$sql=“选择从\u id、到\u id、内容 来自“直接消息” 其中“to_id`=?”; 如果($stmt=$conn->prepare($sql)){ $stmt->bind_参数('i',$myID); /*执行语句*/ $stmt->execute(); /*绑定结果变量*/ $stmt->bind_result($from,$to,$text); /*获取值*/ 而($stmt->fetch()){ 回声“
- ”; echo“消息来源:“.GetUserName($from)。””; 回显“消息内容:“.$text.””; 回声“
”; } /*结束语*/ $stmt->close(); }否则{ echo“准备失败:(“$conn->errno.”“$conn->error; }
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;
}
/* 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();