- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想看一个示例,说明如何使用 bind_result
与 get_result
进行调用,以及使用其中一个的目的是什么。
还有各自的优缺点。
使用这两者的限制是什么,有区别吗。
最佳答案
虽然这两种方法都适用于 *
查询,但是当使用 bind_result()
时,列通常在查询中明确列出,因此可以在分配时引用列表bind_result()
中的返回值,因为变量的顺序必须严格匹配返回行的结构。
$query1
的示例 1 使用 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
的示例 2 使用 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>';
}
优点:
缺点:
优点:
fetch_all()
方法一次返回所有返回的行缺点:
关于php - 如何使用 bind_result 与 get_result 的示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30815774/
这个问题已经有答案了: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in p
我有一个由联合较小查询构建的动态查询。我使用 union 是因为我需要从一个大列表中的几个表中获取数据。这些表有很多共同点,但我仍然需要单独表中的数据。因此我认为这个解决方案似乎是最好的解决方案。到目
我正在为返回多个结果的查询使用准备好的语句,我希望将这些结果放在一个数组中。但是 bind_result 不适用于数组,所以我是这样做的: $read_items = $db->stmt_init()
我正在尝试将 mysqli 与 bind_result 一起使用,但我得到的只是空值。我的 $stmt 行数大于 0,所以我确实有一些数据。 我真的不明白 bind_result 应该有什么值(val
我将此 PHP 文件存储在服务器中。它为数据库创建一个新用户。注册成功但响应信息始终为NULL。 这是我在其中发布值的 register.php 文件 FALSE); if (($_POST['n
在 db_handler.php 页面 public function getAllUserList($user_id) { $stmt = $this->conn->prepare("SEL
这个问题在这里已经有了答案: PHP mysql_stmt::fetch() gives PHP Fatal error memory exhausted (1 个回答) 关闭 2 年前。 我正在尝
我试图在查询中输出从数据库中获取的变量,但未返回任何内容。使用 MYSQLi 预处理语句。 请看下面的代码: $stmt = $con->prepare("SELECT first_name, las
我在尝试绑定(bind)我的结果时遇到问题。 PHP 不断输出以下错误 Warning: mysqli_stmt::bind_result(): Number of bind variables do
我是 php 和 mysql 的新手。我试图从 php 创建一个 rest api,因为我的服务器没有安装 mysqlnd,我必须使用 bind_result 和 fetch。 $stmt = $th
以下代码产生错误:注意: undefined variable :morrisons。 我完全不知道为什么会发生这个错误,这似乎是实际分配的变量有问题,因为它没有被分配。 $sql = "SELECT
这个问题已经有答案了: Is there a way to run store_result and get_result together (2 个回答) 已关闭去年。 我在这个问题上挣扎了一段时间
您好,我正在尝试使用在 ec2 实例上运行的 android 和 php 制作一个登录应用程序,但即使我输入了正确的凭据,我也无法登录。 所以我开始打印查询,如果我打印 $user 我得到的只是“1”
我正在尝试使用 mysqli 获取在我的数据库表中找到的 int。然后我使用 bind_result() 绑定(bind)结果。但是,当我尝试使用该值时,我只是得到一个 0。我该如何处理? 我的代码是
我做了一个准备好的声明,应该登录一个用户,一切正常,但我遇到了 bind_result() 的问题。我的意思是它根本不起作用,我无法获取查询结果并将其分配给 SESSION 这是我的代码: sessi
我正在尝试遍历我的 bind_results,但它并没有像我需要的那样工作。我已经尝试了 20 种不同的方法,结果总是一样的。我只得到 $survey_user_id 和 $survey_result
我想知道如何重写/修复以下代码。 基本上,我有一个名为 DB 的数据库和一个名为 poems 的表。我想获取 poems 表中具有特定 ID 的所有数据(即谁写了这首诗,它具有的标签,它的内容,它的创
我正在寻找一种在不知道列数的情况下从 mysqli 准备语句中获取完整行的方法 这是我测试成功的 if ($stmt = $con->prepare("SELECT user_id FROM user
我有一个 mySQL 表定义为: CREATE TABLE IF NOT EXISTS `coupons` ( `coupon_id` int(11) NOT NULL auto_increm
我想看一个示例,说明如何使用 bind_result 与 get_result 进行调用,以及使用其中一个的目的是什么。 还有各自的优缺点。 使用这两者的限制是什么,有区别吗。 最佳答案 虽然这两种方
我是一名优秀的程序员,十分优秀!