Home  >  Article  >  Backend Development  >  How to delete php database records

How to delete php database records

PHPz
PHPzOriginal
2023-04-04 09:01:23354browse

In PHP development, we often need to operate the database, including deleting database records. This article will introduce how to delete database records in PHP.

1. Connect to the database

To connect to the database in PHP, you can use extensions such as mysqli or PDO. Here we take the mysqli extension as an example.

<?php
    // 数据库连接配置
    $servername = "localhost";
    $username = "root";
    $password = "123456";
    $database = "testdb";

    // 创建连接
    $conn = new mysqli($servername, $username, $password, $database);

    // 检查连接
    if ($conn->connect_error) {
        die("连接失败: " . $conn->connect_error);
    }
?>

2. Delete database records

Deleting database records requires the use of the DELETE statement. The syntax is as follows:

DELETE FROM table_name WHERE condition;

Among them, table_name is the name of the data table, and condition is the selected record. For example, if we want to delete the record with id 1 from the database table named users, we can write like this:

DELETE FROM users WHERE id=1;

In PHP, we can use the mysqli_query function to execute SQL statements. For example, if we want to delete the record with id 1 in the database table named users in PHP, we can write it like this:

<?php
    // 删除记录
    $sql = "DELETE FROM users WHERE id=1";
    if ($conn->query($sql) === TRUE) {
        echo "记录删除成功";
    } else {
        echo "出现错误:" . $conn->error;
    }

    // 关闭连接
    $conn->close();
?>

The above code will output "Record deleted successfully". If an error occurs, an error message will be output. .

3. Delete database records in batches

In addition to deleting individual records, sometimes we need to delete records in batches. This can be achieved using the IN operator and arrays. For example, if we want to delete the records with IDs 1, 2, and 3 in the database table named users, we can write it like this:

<?php
    // 删除记录
    $ids = array(1, 2, 3);
    $idsString = implode(&#39;,&#39;, $ids);
    $sql = "DELETE FROM users WHERE id IN ($idsString)";
    if ($conn->query($sql) === TRUE) {
        echo "记录删除成功";
    } else {
        echo "出现错误:" . $conn->error;
    }

    // 关闭连接
    $conn->close();
?>

The above code will output "Record deleted successfully". If an error occurs, an error will be output. information.

4. Summary

This article introduces how to delete database records in PHP, including single deletion and batch deletion. It should be noted that when performing database operations, you must pay attention to security issues to prevent SQL injection attacks.

The above is the detailed content of How to delete php database records. 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