How can HTML formatting affect the output of PHP code when displaying data from a MySQL database?
HTML formatting can affect the output of PHP code when displaying data from a MySQL database by altering the way the data is presented on the webpage. To ensure proper formatting and display of the data, you can use HTML tags within the PHP code to structure the output as needed, such as using <table> tags for tabular data or <div> tags for styling.
<?php
// Connect to MySQL database
$connection = mysqli_connect('localhost', 'username', 'password', 'database');
// Retrieve data from database
$query = "SELECT * FROM table";
$result = mysqli_query($connection, $query);
// Display data with HTML formatting
echo "<table>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['column1'] . "</td>";
echo "<td>" . $row['column2'] . "</td>";
echo "</tr>";
}
echo "</table>";
// Close database connection
mysqli_close($connection);
?>