What are common reasons for not displaying database records in PHP scripts?

Common reasons for not displaying database records in PHP scripts include errors in the SQL query, connection issues with the database, or incorrect handling of the fetched data. To solve this issue, ensure that the SQL query is correct, the database connection is established successfully, and the fetched data is properly processed and displayed.

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

// Execute SQL query to fetch records
$sql = "SELECT * FROM table_name";
$result = $conn->query($sql);

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

$conn->close();
?>