Detailed explanation of PHP PDO usage: connecting to the database and performing query operations

WBOY
Release: 2024-03-19 10:58:02
Original
1068 people have browsed it

PHP PDO用法详解:连接数据库和执行查询操作

Detailed explanation of PHP PDO usage: connecting to the database and performing query operations

PHP PDO (PHP Data Objects) is a general method for accessing the database, which provides Object-oriented code interface that can interact with a variety of databases. This article will introduce in detail how to use PHP PDO to connect to the database and perform query operations, and provide specific code examples.

1. Connect to the database

First, we need to establish a connection to the database. It is very simple to connect to the database using PDO. We only need to provide the relevant information of the database (such as host name, user name, password, database name, etc.). The following is a sample code to connect to the database:

setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); //Set the error mode to throw an exception echo 'Database connection successful! '; } catch (PDOException $e) { echo 'Database connection failed:' . $e->getMessage(); } ?>
Copy after login

In the above code, we first set the relevant information of the database, then use the PDO class to instantiate a PDO object, and pass in the host name, database name, user name and password. Then set the error mode of PDO to throw an exception, so that an exception will be thrown when there is a problem with the database connection. Finally, output the connection success or failure information.

2. Perform query operations

After connecting to the database, we can use PDO to perform query operations. PDO provides a variety of methods to execute queries, the most commonly used are the prepare method and execute method. The following is a simple query operation example:

prepare($sql); $stmt->bindParam(':id', $id, PDO::PARAM_INT); $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); if ($result) { foreach ($result as $row) { echo 'ID: ' . $row['id'] . ', Name: ' . $row['name'] . '
'; } } else { echo 'No matching record found. '; } } catch (PDOException $e) { echo 'Query failed:' . $e->getMessage(); } ?>
Copy after login

In the above code, we define a query statement $sql and use the prepare method to create a PDOStatement object $stmt. Then use the bindParam method to bind the value of the query parameter $id to 1, and use the execute method to perform the query operation. Finally, use the fetchAll method to obtain the query results, and output the id and name field values of each row of results through a loop.

Summary:

This article introduces how to use PHP PDO to connect to the database and perform query operations, including establishing database connections, performing query operations, etc. By understanding and mastering the usage of PDO, we can operate the database more flexibly and write safer and more reliable database operation code. Hope this article helps you!

The above is the detailed content of Detailed explanation of PHP PDO usage: connecting to the database and performing query operations. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!