問題描述
所以,現在我有一個 PHP 函數,它利用 PDO 返回特定表的第一行.這工作正常,但我想返回所有信息,同時能夠組織所有信息.
So, right now I have a PHP function that utilizes PDO to return the first row of a specific table. That works fine, but I want to return all of the information, while being able to organize it all.
我有表 zip__admins
,我正在嘗試從表中返回 first_name
和 last_name
.有了這些信息,我在登錄頁面上有一個按鈕,要求用戶選擇他們的姓名(每個人都有自己的按鈕)進行登錄.截至目前,我將返回一個結果,而不是兩個結果.如何修改以下代碼以返回兩個結果,并將數據輸入到模板參數中.
I have the table zip__admins
and I'm trying to return the first_name
and last_name
from the table. With this information, I have a button on the login page asking the user to select their name (each person gets their own button) to sign in. As of now, I'm returning one result, instead of two results. How can I modify the below code to return two results, and input the data into a templating parameter.
final public function fetchAdminInfo() {
global $zip, $db, $tpl;
$query = $db->prepare('SELECT first_name, last_name FROM zip__admins');
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
$tpl->define('admin: first_name', $result['first_name']);
$tpl->define('admin: last_name', $result['last_name']);
}
這是我的桌子:![SQL 表][1]
Here is my table: ![SQL Table][1]
推薦答案
需要使用fetchAll()
$result = $query -> fetchAll();
foreach( $result as $row ) {
echo $row['first_name'];
echo $row['last_name'];
}
這篇關于PDO 返回所有行的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!