是否可以将 store_result() 和 bind_result() 与 PHP PDO 一起使用?

时间:2023-03-06
本文介绍了是否可以将 store_result() 和 bind_result() 与 PHP PDO 一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的问题相当简单:是否可以将 store_result()bind_result() 与 PHP PDO 一起使用?

My question is fairly straightforward: Is it possible to use store_result() and bind_result() with PHP PDO?

这是我遇到的一些示例代码:

Here is some example code I came across:

$stmt = $mysqli->prepare("SELECT id, username, password, salt FROM members WHERE email = ? LIMIT 1")) { 
  $stmt->bind_param('s', $email); // Bind "$email" to parameter.
  $stmt->execute(); // Execute the prepared query.
  $stmt->store_result();
  $stmt->bind_result($user_id, $username, $db_password, $salt); // get variables from result.
  $stmt->fetch();

我已经看到这些在 mysqli 的上下文中使用,但没有在 PHP PDO 中使用.store_result()bind_result() 在 www.php.net 上的 mysqli 中被引用.我很好奇它们是否有效,或者是否有类似的方法.

I have seen these used in the context of mysqli, but not with PHP PDO. store_result() and bind_result() are referenced in mysqli on www.php.net. I'm curious if they are valid, or if there are comparable methods.

显然这两种方法之间有一些转换.我的假设是 store_resultbind_result() 类似于 PDO 的 fetch() 命令.

Obviously there is some translation between the two methods. My assumption is that store_result and bind_result() are similar to PDO's fetch() commands.

推荐答案

重点是,在 PDO 中既不使用 store_result() 也不使用 bind_result() 绝对没有意义.
只需获取您的数据并在您希望的任何地方使用它.这就是 PDO 的意义所在.

The point is, there is absolutely no point in using neither store_result() nor bind_result() with PDO.
Just get your data and use it anywhere you wish. That's the very point of PDO.

$sql  = "SELECT id, username, password, salt FROM members WHERE email = ? LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->execute(array($email));
$row  = $stmt->fetch();

现在您的用户数据位于 $row 数组中.

Now you have your userdata in $row array.

将结果行存储在单独的变量中现在很少实践.但是如果你更喜欢这种古老的数据处理方式,你可以添加

Storing resulting row in separate variables is very seldom practice nowadays. But if you prefer such ancient way of dealing with data, you could add

extract($row);

到上面的代码来获取你的全局变量.

to the above code to get your global variables.

mysqli-way 的主要问题是它极难以任何抽象级别使用.

The main problem with mysqli-way is that it is extremely hard to use it with whatever level of abstraction.

在现实生活中,我们从不全局范围中调用数据库,而是在这样的函数中调用

In the real life we never call for database in the global scope, but rather in a function like this

function getUserData() {
    // getting data
    return $array;
}

出于某种原因,这个简单但经常使用的代码在 mysqli 中变得非常困难!在现实生活中,我们不需要单独的变量,而是需要返回单个数组.但是对于mysqli,我们需要先获取这些变量,然后将它们放入数组中,然后才返回它们.简直疯了!

and for some reason this simple yet mostly used code become extremely difficult with mysqli! In the real life we don't need separate variables but rather single array to be returned. But with mysqli we need to get these variables first, then put them in array and only then return them. Just crazy!

这篇关于是否可以将 store_result() 和 bind_result() 与 PHP PDO 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何解决 PHP 中的“无法通过引用传递参数"错误? 下一篇:PHP mysqli准备语句不起作用

相关文章