如何读取fetch(PDO :: FETCH_ASSOC); [英] How to read fetch(PDO::FETCH_ASSOC);

查看:66
本文介绍了如何读取fetch(PDO :: FETCH_ASSOC);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP构建Web应用程序,并且正在使用memcached从数据库存储用户数据.

I am trying to build a web application using PHP and I am using memcached for storing user data from the DB.

例如,假设我有以下代码:

For example lets say that I have this code:

 $sql    = "SELECT * FROM users WHERE user_id = :user_id";
$stmt   = $this->_db->prepare($sql);
$result = $stmt->execute(array(":user_id" => $user_id));
$user   = $stmt->fetch(PDO::FETCH_ASSOC);

我不太确定如何读取$user变量并从中获取数据.我将需要能够阅读电子邮件和密码"列.

I am not really sure how to read the $user variable and get the data out of it. I will need to be able to read the email and password column.

我希望有人可以向我解释这是如何工作的.

I was hoping that someone could explain to me how this works.

谢谢

推荐答案

PDOStatement :: fetch 从结果集中返回一行.参数 PDO::FETCH_ASSOC 告诉PDO返回结果作为关联数组.

PDOStatement::fetch returns a row from the result set. The parameter PDO::FETCH_ASSOC tells PDO to return the result as an associative array.

数组键将与您的列名匹配.如果您的表包含列"email"和"password",则数组的结构如下:

The array keys will match your column names. If your table contains columns 'email' and 'password', the array will be structured like:

Array
(
    [email] => 'youremail@yourhost.com'
    [password] => 'yourpassword'
)

要从电子邮件"列中读取数据,请执行以下操作:

To read data from the 'email' column, do:

$user['email'];

和密码":

$user['password'];

这篇关于如何读取fetch(PDO :: FETCH_ASSOC);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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