Home >Backend Development >PHP Tutorial >How to use database functions for data query and operation in PHP?
How to use database functions in PHP for data query and operation?
1. Connect to the database
In PHP, we use the mysqli extension to connect to the MySQL database. First, we need to use the mysqli_connect function to establish a connection to the database. This function has four parameters, namely database host address, user name, password and database name. The code example is as follows:
$host = 'localhost'; $username = 'root'; $password = '123456'; $dbname = 'test'; $conn = mysqli_connect($host, $username, $password, $dbname); if (!$conn) { die("连接数据库失败:" . mysqli_connect_error()); }
2. Query data
After successfully connecting to the database, we can use the mysqli_query function to execute the SQL query statement and obtain the query results. The following is an example of querying data:
$sql = "SELECT * FROM users"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { while ($row = mysqli_fetch_assoc($result)) { echo "ID: " . $row['id'] . ", 姓名: " . $row['name'] . ", 年龄: " . $row['age'] . "<br>"; } } else { echo "没有查询到数据。"; } mysqli_free_result($result);
In the above code, we first define a SELECT statement, then use the mysqli_query function to execute the query, and assign the result to the $result variable. Use the mysqli_num_rows function to get the number of rows in the query result, and then use the mysqli_fetch_assoc function to get each row of data in a loop and operate on it.
3. Insert data
In addition to querying data, we can also use SQL statements to insert data into the database. The following is an example of inserting data:
$name = "张三"; $age = 20; $sql = "INSERT INTO users (name, age) VALUES ('$name', '$age')"; if (mysqli_query($conn, $sql)) { echo "插入数据成功。"; } else { echo "插入数据失败:" . mysqli_error($conn); }
In the above code, we define an INSERT statement to perform the insert operation through the mysqli_query function. If the insertion is successful, "Inserting data successfully" will be output, otherwise an error message will be output.
4. Update data
If we need to update the data in the database, we can use the UPDATE statement to complete it. The following is an example of updating data:
$id = 1; $name = "李四"; $age = 25; $sql = "UPDATE users SET name='$name', age='$age' WHERE id=$id"; if (mysqli_query($conn, $sql)) { echo "更新数据成功。"; } else { echo "更新数据失败:" . mysqli_error($conn); }
In the above code, we first define an UPDATE statement, and then perform the update operation through the mysqli_query function. If the update is successful, "Update data successfully" will be output, otherwise an error message will be output.
5. Delete data
If we need to delete data from the database, we can use the DELETE statement to complete it. The following is an example of deleting data:
$id = 1; $sql = "DELETE FROM users WHERE id=$id"; if (mysqli_query($conn, $sql)) { echo "删除数据成功。"; } else { echo "删除数据失败:" . mysqli_error($conn); }
In the above code, we define a DELETE statement and then perform the delete operation through the mysqli_query function. If the deletion is successful, "Data deleted successfully" will be output, otherwise an error message will be output.
Summary:
When using PHP for database operations, we need to connect to the database first, and then use the mysqli_query function to execute SQL statements for data query, insertion, update, and delete operations. Through these basic database functions, we can easily perform data addition, deletion, modification and query operations.
The above is the detailed content of How to use database functions for data query and operation in PHP?. For more information, please follow other related articles on the PHP Chinese website!