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();
Related Questions
- Why is the filesize() function in PHP not suitable for remote files, and what alternative method can be used to determine the file size for remote downloads?
- How can the use of isset() and empty() functions in PHP improve the validation process for form fields?
- What are the best practices for handling different versions of a webpage in PHP without using multiple HTML files?