How can PHP be used to display data from a database in a visually appealing format, such as using tables or divs?
To display data from a database in a visually appealing format using PHP, you can fetch the data from the database and then use HTML tables or divs to structure and style the output. You can use PHP to loop through the database results and generate the necessary HTML code to display the data in a visually appealing way.
<?php
// Connect to the database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Check connection
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
// Fetch data from the database
$sql = "SELECT * FROM table_name";
$result = mysqli_query($connection, $sql);
// Display data in a table
echo "<table>";
echo "<tr><th>ID</th><th>Name</th><th>Email</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>".$row['id']."</td><td>".$row['name']."</td><td>".$row['email']."</td></tr>";
}
echo "</table>";
// Close connection
mysqli_close($connection);
?>