How do I delete a specific data from a database in PHP?
To delete a specific data entry from a database in PHP, you can use SQL statements. Here is an example code:
<?php
// 连接数据库
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检查连接是否成功
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// 删除数据
$id = 1; // 要删除的数据的ID
$sql = "DELETE FROM table_name WHERE id = $id";
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
// 关闭连接
mysqli_close($conn);
?>
In the example above, we start by connecting to the database, then we execute a DELETE statement to delete data with a specified ID. Finally, we use the mysqli_query function to execute the SQL statement and output the corresponding result. Lastly, we close the database connection.