問題描述
我正在嘗試使用 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);"的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!