Home > Backend Development > PHP Problem > PHP query is being modified

PHP query is being modified

PHPz
Release: 2023-05-06 19:56:06
Original
548 people have browsed it

PHP is a commonly used programming language that can be used to build web applications. When developing web applications, data query and modification are often required. In this article, we will introduce how to use PHP for query and modification operations.

1. Connect to the database

Before using PHP to perform database operations, we need to connect to the database first. The following is a sample code for connecting to a MySQL database:

//定义数据库连接信息
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";

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

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

2. Query data

  1. SELECT statement

Use the SELECT statement to extract data from the database. The following is an example of a simple SELECT statement:

$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

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

Use the WHERE clause to extract data by specifying conditions. The following is an example of a SELECT statement with a WHERE clause:

$sql = "SELECT * FROM table_name WHERE age=25";
$result = $conn->query($sql);

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

Use the ORDER BY clause to sort the results by specified columns Sort. The following is an example of a SELECT statement with an ORDER BY clause:

$sql = "SELECT * FROM table_name ORDER BY age DESC";
$result = $conn->query($sql);

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

3. Modify data

Use the UPDATE statement to modify the data in the database. The following is an example of a simple UPDATE statement:

$sql = "UPDATE table_name SET age=30 WHERE id=1";

if ($conn->query($sql) === TRUE) {
    echo "记录更新成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
Copy after login

Please note that the WHERE clause should be used to specify the records to be changed when executing the UPDATE statement, otherwise all records will be changed.

4. Summary

In this article, we introduced how to use PHP for data query and modification. To query data, we use SELECT statement and can filter and sort the results using WHERE clause and ORDER BY clause. To modify data we use UPDATE statement and should specify the record to be changed using WHERE clause. Before doing any database operations, we need to connect to the database first.

Using PHP for database operations can greatly simplify the development process of web applications and help us manage data more effectively.

The above is the detailed content of PHP query is being modified. 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