How can the use of LIMIT 1 in SQL queries prevent accidental deletion of multiple records in PHP?
Using LIMIT 1 in SQL queries ensures that only one record is affected by the operation, preventing accidental deletion of multiple records. This is a safety measure to avoid unintended consequences of executing queries that could potentially impact multiple rows. By specifying LIMIT 1, we guarantee that only one record will be deleted, updated, or inserted at a time.
// Example of a SQL DELETE query with LIMIT 1 to prevent accidental deletion of multiple records
$sql = "DELETE FROM table_name WHERE id = :id LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();