php PDO 返回所有行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18435317/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO Return All Rows
提问by Intact Dev
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.
所以,现在我有一个 PHP 函数,它利用 PDO 返回特定表的第一行。这工作正常,但我想返回所有信息,同时能够组织所有信息。
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.
我有桌子zip__admins
,我正试图从桌子上返回first_name
和last_name
。有了这些信息,我在登录页面上有一个按钮,要求用户选择他们的姓名(每个人都有自己的按钮)进行登录。截至目前,我将返回一个结果,而不是两个结果。如何修改以下代码以返回两个结果,并将数据输入到模板参数中。
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']);
}
Here is my table:
这是我的表:
回答by Ravi Thapliyal
You need to use fetchAll()
你需要使用 fetchAll()
$result = $query -> fetchAll();
foreach( $result as $row ) {
echo $row['first_name'];
echo $row['last_name'];
}