はじめに
省略。
コード
<?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);