Home  >  Article  >  Backend Development  >  How to delete a record from database in PHP

How to delete a record from database in PHP

zbt
zbtOriginal
2023-08-23 15:44:091221browse

PHP connects to the database, then constructs a SQL query statement to delete the record, and finally closes the database connection to delete a record in the database. Detailed introduction: 1. For a record in the database, use the mysqli extension to connect to the MySQL database; 2. Construct a SQL query statement to delete the record, and use the DELETE statement to delete the record with id 1 in the table named mytable; 3. Close the database Connection, use the close() method to close the database connection.

How to delete a record from database in PHP

The operating environment of this tutorial: windows10 system, php8.1.3 version, DELL G3 computer.

PHP is a widely used server-side scripting language for developing dynamic web pages and web applications. When developing web applications, you often need to interact with databases. Database is one of the key components used to store and manage data. In some cases, we may need to delete a record in the database. This article will introduce how to use PHP to delete a record in the database.

1. We need to connect to the database. In PHP we can use extensions like mysqli or PDO to connect to the database. Here we use the mysqli extension to connect to the MySQL database. The following is a sample code:

$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "mydatabase";
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}

In the above code, we used localhost as the server name, root as the username, password as the password, and mydatabase as the database name. You need to modify these values ​​according to your actual situation.

2. We need to construct a SQL query statement to delete a record in the database. The following is a sample code:

$sql = "DELETE FROM mytable WHERE id = 1";
if ($conn->query($sql) === TRUE) {
echo "记录删除成功";
} else {
echo "Error: " . $sql . "
" . $conn->error;
}

In the above code, we have used the DELETE statement to delete the record with id 1 in the table named mytable. You need to modify the table name and conditions according to your actual situation.

3. We need to close the database connection. The following is a sample code:

$conn->close();

In the above code, we use the close() method to close the database connection.

To sum up, the above are the steps to delete a record in the database using PHP. First, we need to connect to the database, then build a SQL query to delete records, and finally close the database connection. With these steps we can easily delete a record in the database.

The above is the detailed content of How to delete a record from database in 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