PDO返回所有行 [英] PDO Return All Rows

查看:102
本文介绍了PDO返回所有行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,现在我有一个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_namelast_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']);
}

这是我的桌子:

推荐答案

您需要使用fetchAll()

$result = $query -> fetchAll();

foreach( $result as $row ) {
    echo $row['first_name'];
    echo $row['last_name'];
}

这篇关于PDO返回所有行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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