How can the WHERE clause be effectively used in UPDATE statements in PHP to target specific records for modification?
When using the WHERE clause in UPDATE statements in PHP, you can specify specific conditions that must be met for the update to occur. This allows you to target and modify only the records that meet the specified criteria. By using the WHERE clause effectively, you can ensure that your updates are applied only to the desired records in the database.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Update records that meet a specific condition
$sql = "UPDATE users SET status = 'active' WHERE id = 1";
if ($conn->query($sql) === TRUE) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $conn->error;
}
// Close connection
$conn->close();
?>