What is the correct syntax for deleting MySQL data in PHP?
To delete MySQL data in PHP, you can use the DELETE statement in a SQL query. Make sure to connect to your MySQL database using mysqli or PDO, and then execute the DELETE query to remove the desired data from the table. Remember to include a WHERE clause to specify the condition for which rows to delete.
<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Delete data from table
$sql = "DELETE FROM table_name WHERE condition";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
?>
Related Questions
- What are some potential pitfalls to consider when using if-else statements in PHP to check for multiple time ranges?
- What are the best practices for troubleshooting PHPBB2 connection refused errors in browsers like Opera?
- Are there alternative approaches to dynamically generating and executing PHP functions that are safer and more secure than using "eval()"?