How can PHP be used to display text with paragraphs from a MySQL table?
To display text with paragraphs from a MySQL table using PHP, you can fetch the data from the database and use the nl2br() function to convert newline characters to HTML line breaks. This will ensure that paragraphs are displayed correctly on the webpage.
<?php
// Connect to MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");
// Fetch text data from MySQL table
$query = "SELECT text_content FROM table_name";
$result = mysqli_query($connection, $query);
// Display text with paragraphs
while($row = mysqli_fetch_assoc($result)) {
echo nl2br($row['text_content']);
}
// Close database connection
mysqli_close($connection);
?>
Keywords
Related Questions
- How can user permissions be managed efficiently in a PHP-based CMS to ensure real-time updates?
- What are the best practices for structuring SQL queries in PHP to avoid errors and improve code readability?
- What is the significance of using JOINS in SQL for associating comments with images in a database?