What improvements can be made to the PHP code to ensure all database records are displayed correctly?

The issue may be related to how the database records are fetched and displayed in the PHP code. To ensure all database records are displayed correctly, we can make sure that the query fetches all records, and then loop through each record to display its data.

<?php
// Connect to the database
$connection = new mysqli("localhost", "username", "password", "database");

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

// Fetch all records from the database
$query = "SELECT * FROM table_name";
$result = $connection->query($query);

// Display records
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "ID: " . $row["id"] . " - Name: " . $row["name"] . "<br>";
    }
} else {
    echo "0 results";
}

// Close connection
$connection->close();
?>