How can one troubleshoot a PHP script that is not displaying user data as expected?

If a PHP script is not displaying user data as expected, the issue may be with how the data is being retrieved or processed. To troubleshoot, check the database connection, query for the correct data, and ensure that the data is being displayed correctly in the HTML output. Additionally, check for any errors in the PHP code that may be preventing the data from being displayed.

<?php
// Check the database connection
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Query for the user data
$sql = "SELECT * FROM users WHERE id = $user_id";
$result = $conn->query($sql);

// Display the user data
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "Name: " . $row["name"]. "<br>";
        echo "Email: " . $row["email"]. "<br>";
    }
} else {
    echo "No user found";
}

$conn->close();
?>