What improvement could be made to the PHP script to output all data records?

The issue with the current PHP script is that it is only outputting the first data record from the database. To output all data records, we can use a loop to iterate through each record and echo out the data. This can be achieved by fetching all records from the database and then looping through each record to display the data.

// 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 data records from the database
$query = "SELECT * FROM table_name";
$result = $connection->query($query);

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

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