How to operate database in PHP?

PHPz
Release: 2023-05-21 09:26:02
Original
2187 people have browsed it

In web development, the database is a very important component. As a scripting language, PHP is ideal for writing code that interacts with databases. PHP provides a variety of ways to operate the database, including native MySQL functions, PDO, and higher-level ORM frameworks. In this article, we will explain how to work with databases in PHP.

  1. PHP interacts with MySQL

MySQL is a popular relational database management system and one of the most commonly used databases for PHP. PHP provides a series of native MySQL functions that can be used to connect to the MySQL database, query data, insert data, modify data and delete data. The following is a basic sample code to connect to a local MySQL database and query all data in the users table:

 0) {
        while ($row = mysqli_fetch_array($result)) {
            echo $row['id'] . " " . $row['name'] . " " . $row['email'];
            echo "
"; } // 释放结果集资源 mysqli_free_result($result); } else { echo "No records found."; } } else { echo "Error: Could not able to execute $sql. " . mysqli_error($link); } // 关闭连接 mysqli_close($link); ?>
Copy after login

In the above code, we first use the mysqli_connect() function to connect to the MySQL database. We then query all the data in the users table, using the mysqli_query() function to execute the SQL statement and return the result set. Finally, we use the mysqli_fetch_array() function to obtain the query results row by row and output them to the page. Use the mysqli_free_result() function to release result set resources, and use the mysqli_close() function to close the connection.

  1. PHP PDO interacts with MySQL

PDO is a lightweight database abstraction layer of PHP that supports a variety of databases and provides simpler and more efficient database operations. Methods. Using PDO, we can use a single code base to operate multiple types of databases without changing the code. The following is a basic sample code, connect to the local MySQL database, query all data in the users table:

getMessage());
}
 
// 查询 users 表中的所有数据
$sql = "SELECT * FROM users";
if ($result = $pdo->query($sql)) {
    // 打印结果
    if ($result->rowCount() > 0) {
        while ($row = $result->fetch()) {
            echo $row['id'] . " " . $row['name'] . " " . $row['email'];
            echo "
"; } } else { echo "No records found."; } } else { echo "Error: Could not able to execute $sql. " . $pdo->errorInfo(); } // 关闭连接 $pdo = null; ?>
Copy after login

In the above code, we first create a PDO instance, using $dsn, $username and $password Connect to the MySQL database. We then query all the data in the users table, using the $pdo->query() function to execute the SQL statement and return the result set. Finally, we use the $result->fetch() function to fetch the query results row by row and output them to the page. Close the connection using the $pdo = null; statement.

  1. PHP ORM framework interacts with MySQL

ORM (Object Relational Mapping, object relational mapping) is a software framework used to establish mapping between objects and relational databases . The ORM framework is a high-level library that helps us develop and maintain applications more quickly. There are multiple ORM frameworks to choose from in PHP, including Laravel's Eloquent, Symfony's Doctrine, Yii2's ActiveRecord, etc. The following is a basic sample code that uses Eloquent ORM to connect to a local MySQL database and query all data in the users table:

addConnection([
    "driver" => "mysql",
    "host" => "localhost",
    "database" => "database_name",
    "username" => "username",
    "password" => "password",
]);
 
// 启动 Eloquent ORM
$capsule->bootEloquent();
 
// 查询 users 表中的所有数据
$users = Capsule::table("users")->get();
 
// 打印结果
if (count($users) > 0) {
    foreach ($users as $user) {
        echo $user->id . " " . $user->name . " " . $user->email;
        echo "
"; } } else { echo "No records found."; } ?>
Copy after login

In the above code, we first use Composer to install Eloquent ORM and introduce the corresponding classes . Then we create a Capsule instance and add database connection information. Start the Eloquent ORM using the $capsule->bootEloquent() function. Finally, we use the Capsule::table() function to query all the data in the users table and use a foreach loop to output it to the page row by row.

Conclusion

When operating databases in PHP, we have many choices. You can easily connect to the MySQL database using native MySQL functions, but the amount of code is relatively cumbersome. PDO can be used to connect to many types of databases and provides some efficient operation methods. Using an ORM framework can greatly simplify our code development and provide higher stability and maintainability. We need to choose the most appropriate method based on the specific needs of the project.

The above is the detailed content of How to operate database in PHP?. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!