简体   繁体   English

PDO 从数据库中获取数据

[英]PDO get data from database

I started using PDO recently, earlier I was using just MySQL.我最近开始使用 PDO,之前我只使用 MySQL。 Now I am trying to get all data from database.现在我试图从数据库中获取所有数据。

$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.但它没有显示任何用户,只是一个空白页面。

The PDO method 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() )更新(缺少execute()
Also, it appears you aren't calling the execute() method which needs to happen after you prepare the statement but before you actually fetch the data:此外,您似乎没有调用execute()方法,该方法需要准备语句之后但实际获取数据之前发生:

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

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/>";
}

Your code is missing a call to execute() after prepare() .您的代码在prepare()之后缺少对execute()的调用。 After your prepared statement is executed you can get the array of records using fetchAll() .执行准备好的语句后,您可以使用fetchAll()获取记录数组。

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

// Then in your presentation logic you can use the following code to display the data:
if ($users) {
    foreach ($users as $user) {
        echo $user['username']."<br/>";
    }
} else {
    // Whatever your requirement is to handle the no user case, do it here.
    echo '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/>";
}

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

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