How to read quot;fetch(PDO::FETCH_ASSOC);quot;(如何读取“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 database.
例如,假设我有这个代码:
For example, let’s 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.
这是如何工作的?
推荐答案
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);"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何读取“fetch(PDO::FETCH_ASSOC);"
基础教程推荐
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
