Skip to content
PHP Logo

How can the result of a SQL query be properly displayed in PHP?

To properly display the result of a SQL query in PHP, you can fetch the data from the query result and then iterate through the rows to display the data in a desired format, such as a table or list. You can use functions like mysqli_fetch_assoc() or mysqli_fetch_array() to fetch the data row by row.

php
// Assuming $conn is the mysqli connection object and $query is the SQL query
$result = mysqli_query($conn, $query);

if (mysqli_num_rows($result) > 0) {
    echo "<table>";
    while ($row = mysqli_fetch_assoc($result)) {
        echo "<tr>";
        foreach ($row as $value) {
            echo "<td>" . $value . "</td>";
        }
        echo "</tr>";
    }
    echo "</table>";
} else {
    echo "No results found.";
}