Home  >  Article  >  Backend Development  >  How to query and modify data using PHP

How to query and modify data using PHP

PHPz
PHPzOriginal
2023-04-24 14:50:53630browse

PHP is a widely used server-side scripting language that can be used to build dynamic websites and web applications. Querying and modifying data are common operations in web applications. This article will introduce how to use PHP to query and modify data.

1. Query data

  1. Connect to the database

Before using PHP to query the database, you need to connect to the database first. You can use PHP's own mysqli or PDO extension to complete the database connection. The following is a sample code for connecting to the database using the mysqli extension:

// 连接MySQL数据库
$mysqli = new mysqli('localhost', 'username', 'password', 'database_name');

// 检查连接是否成功
if ($mysqli->connect_errno) {
    die('连接错误:' . $mysqli->connect_error);
}

In the above code, localhost represents the database server name, username and password represent the username and password respectively, and database_name represents the name of the database to be connected. connect_errno and connect_error are error checking functions provided by the mysqli extension.

  1. Perform query operation

After successfully connecting to the database, you can perform query operation. Using the mysqli extension, you can use the query() function to execute SQL query statements, as shown below:

// 执行SQL查询语句
$result = $mysqli->query("SELECT * FROM users");

// 检查查询是否成功
if (!$result) {
    die('查询错误:' . $mysqli->error);
}

In the above code, SELECT * FROM users is a SQL query statement used to query all data from the users table . $result is the result set returned after executing the query operation.

  1. Processing the result set

After executing the query operation, you can use some functions to process the result set. For example, you can use the mysqli_fetch_assoc() function to return a row of data from the result set as an array. The following is a sample code that uses the mysqli_fetch_assoc() function to process the result set:

// 处理结果集
while ($row = $result->fetch_assoc()) {
    echo '用户ID:' . $row['user_id'] . '<br>';
    echo '用户名:' . $row['username'] . '<br>';
    echo '密码:' . $row['password'] . '<br><br>';
}

In the above code, the fetch_assoc() function is a function provided by the mysqli extension and is used to obtain a row of data in the result set. In the while loop, use the mysqli_fetch_assoc() function to obtain each row of data, and then output the obtained data.

2. Modify data

  1. Perform modification operations

Modifying data is achieved by executing SQL statements. Using the mysqli extension, you can use the query() method to execute SQL statements. The following is a sample code to perform a modification operation:

// 执行更新操作
$sql = "UPDATE users SET username = 'new_username' WHERE user_id = 1";
if ($mysqli->query($sql)) {
    echo '更新成功!';
} else {
    echo '更新失败:' . $mysqli->error;
}

In the above code, the UPDATE statement is used to update data. The SET clause is used to specify the fields and values ​​to be updated, and the WHERE clause is used to limit the updated rows. In this example, update the username field in the row with user_id 1 in the users table to new_username.

  1. Processing update results

After performing the update operation, you can check whether the update was successful. Using the mysqli extension, you can determine whether the update operation is successful based on the return value of the query() function. Taking the sample code in the previous section as an example, if the update is successful, echo 'Update successful! '. If the update fails, echo 'Update failed:' . $mysqli->error.

3. Summary

Using PHP to query and modify data is a very common operation. Use mysqli or PDO extensions to easily connect to the database and execute SQL query statements. The query result set can be processed using the mysqli_fetch_assoc() function, and the update result set can be judged based on the return value of the query() function to determine whether the update is successful. Good data processing ability is an essential skill for web developers. I hope this article can be helpful to readers.

The above is the detailed content of How to query and modify data using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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