PHP provides a variety of functions for deleting data from the database, including mysqli_query() and mysqli_affected_rows(). These functions can be used according to the following steps: 1. Establish a database connection; 2. Prepare an SQL query; 3. Execute the query; 4 . Check the number of affected rows.
PHP provides a variety of functions for deleting data from the database. This tutorial will introduce the use of these functions and provide practical examples.
The following are commonly used functions in PHP for deleting database data:
mysqli_query()
: Directly execute SQL DELETE
Query. mysqli_affected_rows()
: Returns the number of deleted rows. To use these functions to delete database data, follow these steps:
mysqli_connect()
Function establishes a connection to the database. DELETE
query, specifying the table and conditions to delete data. mysqli_query()
function to execute the query. mysqli_affected_rows()
function to check how many rows were deleted. The following is an actual case of deleting all records in the table named users
:
<?php // 建立数据库连接 $conn = mysqli_connect('localhost', 'root', '', 'database_name'); // 准备 SQL 查询 $sql = "DELETE FROM users"; // 执行查询 $result = mysqli_query($conn, $sql); // 检查受影响行数 $affected_rows = mysqli_affected_rows($conn); echo "受影响的行数:$affected_rows"; ?>
Executing this code will Delete all records from the users
table.
The above is the detailed content of How to delete data from database using PHP function?. For more information, please follow other related articles on the PHP Chinese website!