是否可以在PHP PDO中使用store_result()和bind_result()? [英] Is it possible to use store_result() and bind_result() with PHP PDO?

查看:55
本文介绍了是否可以在PHP PDO中使用store_result()和bind_result()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题很简单:是否可以将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中却没有.在www.php.net上的mysqli中引用了store_result()bind_result().我很好奇它们是否有效,或者是否有可比的方法.

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!

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆