To delete the last rows from a MySQL table using PHP, you can use an SQL query with the ORDER BY and LIMIT clauses. Here’s an example:
<?php
// Replace 'your_database_name', 'your_username', 'your_password', and 'your_table_name' with your actual database information
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to delete the last N rows (replace N with the number of rows you want to delete)
$numRowsToDelete = 1; // Change this to the number of rows you want to delete
$sql = "DELETE FROM your_table_name ORDER BY id DESC LIMIT $numRowsToDelete";
// Execute the query
if ($conn->query($sql) === TRUE) {
echo "Last $numRowsToDelete rows deleted successfully";
} else {
echo "Error deleting rows: " . $conn->error;
}
// Close the connection
$conn->close();
?>