What steps can be taken to troubleshoot and resolve issues with fetching and displaying multiple database records in PHP?

Issue: When fetching and displaying multiple database records in PHP, it is important to ensure that the database connection is established properly, the query is executed successfully, and the retrieved data is displayed correctly on the webpage.

// Establish a database connection
$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);
}

// Fetch and display multiple database records
$sql = "SELECT * FROM table";
$result = $conn->query($sql);

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

$conn->close();