How can the LIMIT clause in MySQL be used to retrieve only the last 5 rows of a table in PHP?

To retrieve only the last 5 rows of a table in MySQL using the LIMIT clause in PHP, you can use the ORDER BY clause to sort the rows in descending order based on a unique identifier column (such as an auto-incremented primary key) and then apply the LIMIT 5 clause to fetch only the last 5 rows.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM table_name ORDER BY unique_id_column DESC LIMIT 5";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // output data of each row
        echo "Column1: " . $row["column1"]. " - Column2: " . $row["column2"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>