How can PHP code be used to maintain formatting when retrieving data from a MySQL database for display on a webpage?
When retrieving data from a MySQL database to display on a webpage using PHP, you can maintain formatting by using the PHP `nl2br()` function. This function converts new line characters (\n) to HTML line breaks (<br>) so that the text appears correctly formatted on the webpage.
<?php
// Retrieve data from MySQL database
$data = "This is a text with \n new line characters.";
// Display data on webpage with formatting maintained
echo nl2br($data);
?>
Keywords
Related Questions
- What are the limitations of PHP in terms of long-running processes and maintaining stateful connections with external servers like BattlEye?
- How can PHP variables and strings be effectively combined to achieve the desired output of displaying data in alternating columns from a SQL database?
- How can SQL injection vulnerabilities be mitigated when passing values to MySQL queries in PHP?