1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

SQLのdeleteを死ぬほど楽にする関数を作った

Last updated at Posted at 2023-02-15

はじめに

省略。

コード

<?php
function deleteRecord($host, $db, $user, $password, $table, $id) {
    try {
        $dsn = "mysql:host=$host;dbname=$db";
        $pdo = new PDO($dsn, $user, $password);

        $stmt = $pdo->prepare("DELETE FROM $table WHERE id = ?");
        $stmt->execute([$id]);

        if ($stmt->rowCount() == 1) {
            echo "Record deleted successfully";
        } else {
            echo "Record not found";
        }
    } catch (PDOException $e) {
        echo "Error deleting record: " . $e->getMessage();
    }

    $pdo = null;
}
?>

使い方

deleteRecord($host, $db, $user, $password, $table, $id);
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?