How can the retrieved data from a SQL query be properly displayed in PHP?

When retrieving data from a SQL query in PHP, you need to loop through the results and display them in a readable format. One common way to do this is by using a while loop to fetch each row of data and then echoing out the desired columns. You can also format the data using HTML to make it more visually appealing.

// Assume $conn is the database connection object and $sql is the SQL query
$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();