What SQL query can be used to retrieve the top 5 highest rated images from a table in MySQL?

To retrieve the top 5 highest rated images from a table in MySQL, you can use the following SQL query: ```sql SELECT * FROM images_table ORDER BY rating DESC LIMIT 5; ``` This query will select all columns from the `images_table`, order the results by the `rating` column in descending order (highest rated first), and then limit the output to only the top 5 rows. Here is a complete PHP code snippet that executes this query:

<?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);
}

// SQL query to retrieve top 5 highest rated images
$sql = "SELECT * FROM images_table ORDER BY rating DESC LIMIT 5";
$result = $conn->query($sql);

// Output the results
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Image ID: " . $row["image_id"]. " - Rating: " . $row["rating"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>