Deleting data in the database is a very common operation. In this article, we will explain how to delete records from a database using PHP. If you are a PHP developer or are learning PHP development, this article is a must!
1. Connect to the database
First, you need to connect to the MySQL server. For this you can use a PHP function mysqli_connect(). This function requires 4 parameters: hostname, username, password and database name. For example:
$host = "localhost"; $user = "username"; $password = "password"; $db_name = "database_name"; $conn = mysqli_connect($host, $user, $password, $db_name);
2. Check whether the connection is valid
After connecting to the database, you need to check whether the connection is successful. You can use a mysqli_affected_rows() function in PHP. If the return value is positive, the connection is successful. Otherwise, it returns a negative number. The following is a practical example:
if(mysqli_affected_rows($conn) === -1) { die("Error: Connection failed!"); }
3. Prepare SQL statement
Now you need to prepare a SQL statement to delete a record in the database. First, you need to select the tables and records you want to delete. For example, if you have a table called "users", you can use the following code:
$table_name = "users"; $id = $_GET['id']; $sql = "DELETE FROM $table_name WHERE id = $id";
In the above code, we are using the $id variable, which is obtained from the $_GET array, which Contains all parameters sent to the server via the GET method.
Please note that we are using double quotes ("") to surround the variable and adding a $ sign in front of the variable. This will let the PHP interpreter know that this is a variable name and not a string constant.
4. Execute the SQL statement
Next, you need to send the SQL statement to the database and execute it. There is a mysqli_query() function that can help you achieve this. Here is the code sample:
$result = mysqli_query($conn, $sql); if(!$result) { die("Error: Could not delete record!"); }
5. Close the database connection
Finally, after completing the connection and operation with the database, it is best to close the connection. This can be achieved through the mysqli_close() function:
mysqli_close($conn);
Full code example:
$host = "localhost"; $user = "username"; $password = "password"; $db_name = "database_name"; $conn = mysqli_connect($host, $user, $password, $db_name); if(mysqli_affected_rows($conn) === -1) { die("Error: Connection failed!"); } $table_name = "users"; $id = $_GET['id']; $sql = "DELETE FROM $table_name WHERE id = $id"; $result = mysqli_query($conn, $sql); if(!$result) { die("Error: Could not delete record!"); } mysqli_close($conn);
This is how to delete a record from the database using PHP. We hope this article helps you better understand how to handle data and make your PHP development more efficient and optimized.
The above is the detailed content of How to delete records from database using PHP. For more information, please follow other related articles on the PHP Chinese website!