简体   繁体   English

php函数返回数据库表中的用户列表

[英]php function to return list of users in database table

I am trying to get the list of all the users in my table in a function then return the list to a php page. 我正在尝试获取函数中表中所有用户的列表,然后将该列表返回到php页面。 I think I am missing some small detail because i can not get anything returned. 我想我缺少一些小细节,因为我什么也得不到。 I am new to OOP and PDO, so I am having trouble pin pointing the problem. 我是OOP和PDO的新手,所以我很难指出问题所在。

Here is the function 这是功能

public function get_users() {

    $query = $this->db->prepare("SELECT * FROM `users` ");

    try{
                    $results = array();
        $query->execute();
        while($rows = $query->fetch_assoc())
                {
                    $results[] = $rows;
                }
    }catch(PDOException $e){
        die($e->getMessage());
    }

    return $results;

}   

And in the page: 并在页面中:

$user_list  = $users->get_users();
foreach ($user_list as $ul) {
    echo $ul['username'];
}

Any pointers would be a great help. 任何指针都会有很大的帮助。 Thank you 谢谢

Change: 更改:

    while($rows = $query->fetch_assoc())
            {
                $results[] = $rows;
            }

to: 至:

    while($rows = $query->fetch(PDO::FETCH_ASSOC))
            {
                $results[] = $rows;
            }

or: 要么:

    $results = $query->fetchAll(PDO::FETCH_ASSOC);

There is no fetch_assoc method in PDO, that's mysqli. PDO中没有fetch_assoc方法,即mysqli。

First of all, there's no need to prepare that statement. 首先,无需准备该声明。 There aren't any bound parameters, so it doesn't make sense (IMHO). 没有任何绑定参数,因此没有任何意义(恕我直言)。 You can do it, but since you won't be running the query multiple times there no benefit. 可以这样做,但是由于不会多次运行查询,因此没有任何好处。

Second of all, you can set \\PDO::FETCH_ASSOC at instantiation or at fetch. 其次,您可以在实例化或\\PDO::FETCH_ASSOC时设置\\PDO::FETCH_ASSOC Much nicer, again IMHO. 更好,再次恕我直言。

Finally, if you use \\PDOStatment::fetchAll() , you're already returning an array, so no need to build that array. 最后,如果使用\\PDOStatment::fetchAll() ,则您已经在返回一个数组,因此无需构建该数组。

Your new, simpler function would look like this (untested): 您的新的,更简单的函数将如下所示(未经测试):

public function get_users()
{
    try {
        return $this->db
            ->query("SELECT * FROM `users`")
            ->fetchAll(\PDO::FETCH_ASSOC);
    } catch (\PDOException $e) {
        die($e->getMessage());
    }
}

you have forgaten to initialize $results as array and return $results not $results[] 您已经forgaten将$results初始化为数组,并返回$results而不是$results[]

public function get_users() {

    $query = $this->db->prepare("SELECT * FROM `users` ");
    $results=array();
    try{
        $query->execute();
        while($rows = $query->fetch_assoc())
                {
                    $results[] = $rows;
                }
    }catch(PDOException $e){
        die($e->getMessage());
    }

    return $results;//correct your return also

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM