Home > Backend Development > PHP Problem > How to perform database query and modification operations in php

How to perform database query and modification operations in php

PHPz
Release: 2023-04-12 09:47:15
Original
1134 people have browsed it

PHP is a widely used server-side scripting language used for web development.

When developers need to read data from the database or modify the database, PHP provides some simple and powerful functions that make querying and modifying the database easy.

This article will introduce some commonly used database queries and modification operations in PHP, hoping to be helpful to PHP developers.

1. Database connection

Before performing any query or modification operation, you must first connect to the database. PHP provides a standard API for interacting with a variety of databases.

Example:

$servername = "localhost"; // 数据库主机名
$username = "username"; // 数据库用户名
$password = "password"; // 数据库密码
$dbname = "myDB"; // 数据库名

// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
Copy after login

2. Query data

Querying data is the process of retrieving information from the database, which allows developers to use a variety of methods to obtain what they need data. In PHP, you can query data using the following statements:

  1. SELECT statement

The most commonly used statement when retrieving data in a database is the SELECT statement. This statement defines the columns to be retrieved and their order, and can specify filter conditions.

Example:

$sql = "SELECT id, name, email FROM users WHERE age > 18";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Email: " . $row["email"]. "<br>";
    }
} else {
    echo "0 结果";
}
Copy after login
  1. WHERE clause

The WHERE clause is one of the optional parts of the SELECT statement, which is used to specify the query Filter conditions.

Example:

$sql = "SELECT id, name, email FROM users WHERE age > 18 AND gender = 'female'";
$result = $conn->query($sql);
Copy after login

3. Modify data

In addition to retrieving data, you can also perform modification operations. The basic statement to modify data from the database is UPDATE.

  1. UPDATE Statement

The UPDATE statement is used to update existing records in the database.

Example:

$sql = "UPDATE users SET name='New Name' WHERE id=1";

if ($conn->query($sql) === TRUE) {
    echo "数据更新成功";
} else {
    echo "数据更新失败: " . $conn->error;
}
Copy after login
  1. DELETE statement

The DELETE statement is used to delete rows from a table.

Example:

$sql = "DELETE FROM users WHERE id=1";

if ($conn->query($sql) === TRUE) {
    echo "数据删除成功";
} else {
    echo "数据删除失败: " . $conn->error;
}
Copy after login

4. Summary

PHP provides a variety of functions for interacting with the database, including operations such as connecting to the database, querying data, and modifying data. It is important for a PHP developer to understand these basics. By mastering this knowledge, development efficiency can be greatly improved, making development work easier and more efficient.

The above is the detailed content of How to perform database query and modification operations 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