php PDO 从数据库中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13183579/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:55:46  来源:igfitidea点击:

PDO get data from database

phpselectpdowhile-loopfetch

提问by chizijs

I started using PDO recently, earlier I was using just MySQL. Now I am trying to get all data from database.

我最近开始使用 PDO,之前我只使用 MySQL。现在我试图从数据库中获取所有数据。

$getUsers = $DBH->prepare("SELECT * FROM users ORDER BY id ASC");
$getUsers->fetchAll();
if(count($getUsers) > 0){
    while($user = $getUsers->fetch()){
        echo $user['username']."<br/>";
    }
}else{
    error('No users.');
}

But it is not showing any users, just a blank page.

但它没有显示任何用户,只是一个空白页面。

回答by newfurniturey

The PDOmethod fetchAll()returns an array/result-set, which you need to assign to a variable and then use/iterate through that variable:

PDO方法fetchAll()返回一个数组/结果集,您需要将其分配给一个变量,然后使用/迭代该变量:

$users = $getUsers->fetchAll();
foreach ($users as $user) {
    echo $user['username'] . '<br />';
}

UPDATE(missing execute())
Also, it appears you aren't calling the execute()method which needs to happen afteryou prepare the statement but beforeyou actually fetch the data:

UPDATE(失踪execute()
而且,看来你是不是调用execute()它需要做的方法后,你准备的发言,但之前你真正获取数据:

$getUsers = $DBH->prepare("SELECT * FROM users ORDER BY id ASC");
$getUsers->execute();
$users = $getUsers->fetchAll();
...

回答by Abiola Babatunde

This code below will do what you are asking for:

下面的代码将满足您的要求:

$sql = $dbh->prepare("SELECT * FROM users ORDER BY id ASC");
$sql->execute();
while ($result = $sql->fetch(PDO::FETCH_ASSOC)) {
    echo $result['username']."<br/>";
}

回答by Dharman

Your code is missing a call to execute()after prepare(). After your prepared statement is executed you can get the array of records using fetchAll().

您的代码缺少对execute()after的调用prepare()。执行准备好的语句后,您可以使用fetchAll().

$getUsers = $DBH->prepare('SELECT * FROM users ORDER BY id ASC');
$getUsers->execute();
$users = $getUsers->fetchAll();
if ($users) {
    foreach ($users as $user) {
        echo $user['username']."<br/>";
    }
} else {
    trigger_error('No users.');
}

However, in you example you are not passing any variable data to the query and you only execute it once, which defeats a purpose of having prepared statement. You should use prepared statements if you need to have variables in your SQL, for example:

但是,在您的示例中,您没有将任何变量数据传递给查询,并且只执行一次,这违背了准备语句的目的。如果您的 SQL 中需要有变量,您应该使用准备好的语句,例如:

$getUsers = $DBH->prepare('SELECT * FROM users WHERE id=?');
$getUsers->execute([
    $_GET['user_id']
]);
$user = $getUsers->fetch();     // get single user by unique id

If there are no variables then you can simply use query()method.

如果没有变量,那么您可以简单地使用query()方法。

$users = $DBH->query('SELECT * FROM users ORDER BY id ASC')->fetchAll();

If you do not need to fetch all records at once, you can simply loop on the statement with foreach:

如果您不需要一次获取所有记录,您可以简单地使用以下语句循环foreach

$getUsers = $DBH->prepare('SELECT * FROM users WHERE username LIKE ? ORDER BY id ASC');
$getUsers->execute([
    '%' . $_GET['search'] . '%'     // search for username with wildcards
]);
foreach ($getUsers as $user) {
    echo $user['username']."<br/>";
}