Home > Backend Development > PHP Tutorial > How to use PDO for SELECT query? (code example)

How to use PDO for SELECT query? (code example)

藏色散人
Release: 2023-04-05 16:06:01
Original
5158 people have browsed it

There are several ways to use PDO to run SELECT queries. Their differences mainly lie in the existence of parameters, parameter types and result types. I will show examples for each case so that I can choose the most suitable one.

How to use PDO for SELECT query? (code example)

SELECT query without parameters

If there are no variables to be used in the query, we can use the traditional query ()method.

// 选择所有用户
$stmt = $pdo->query("SELECT * FROM users");
Copy after login

This will give us a $stmt object which can be used to get the actual rows.

Get a row

If a query only returns one row, then you can call the fetch() method of the $stmt variable:

//获取最后一个注册用户
$stmt = $pdo->query("SELECT * FROM users ORDER BY id DESC LIMIT 1");
$user = $stmt->fetch();
Copy after login

Note that in In PHP, you can call the method of the returned object, such as:

$user = $pdo->query("SELECT * FROM users ORDER BY id DESC LIMIT 1")->fetch();
Copy after login

Query multiple rows

There are two ways to get the multiple rows returned by the query. The most traditional method is to use the fetch() method in a while loop:

$stmt = $pdo->query("SELECT * FROM users");
while ($row = $stmt->fetch()) {
    echo $row[&#39;name&#39;]."<br />\n";
}
Copy after login

This method can be recommended if the rows must be processed one by one. For example, if such processing is the only action that needs to be taken, or the data needs to be preprocessed in some way before use.

But the best way to get multiple rows of data that will be displayed on the web page is to call the method called fetchAll(). It will put all the rows returned by the query into a PHP array, which can later be used to output the data using a template (this is considered much better than echoing the data directly during the fetch).

The code is as follows:

$data = $pdo->query("SELECT * FROM users")->fetchAll();
foreach ($data as $row) {
    echo $row[&#39;name&#39;]."<br />\n";
}
Copy after login

SELECT query with parameters

But most of the time, we have to use one or two variables in the query, in In this case, we should use a prepared statement (also called a parameterized query) to first prepare a query with parameters (or placeholder markers) and then execute it, sending the variables respectively.

In PDO, we can use positional placeholders and named placeholders at the same time. For simple queries, personally I prefer positional placeholders, I find them less verbose, but it's entirely a matter of personal preference.

SELECT query using positional placeholders

//通过id选择特定的用户
$stmt = $pdo->prepare("SELECT * FROM users WHERE id=?");
$stmt->execute([$id]); 
$user = $stmt->fetch();
Copy after login

SELECT query using named placeholders

// 通过id选择特定的用户
$stmt = $pdo->prepare("SELECT * FROM users WHERE id=:id");
$stmt->execute([&#39;id&#39; => $id]); 
$user = $stmt->fetch();
Copy after login

This article This article is an introduction to using PDO for SELECT query. I hope it will be helpful to friends in need!

The above is the detailed content of How to use PDO for SELECT query? (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
pdo
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template