How can PHP code be properly integrated with HTML elements like textarea to display database query results?

To properly integrate PHP code with HTML elements like textarea to display database query results, you can fetch the data from the database using PHP and then echo it within the textarea element in your HTML code. This way, the query results will be displayed within the textarea when the page is loaded.

<?php
// Assuming you have already established a database connection
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);

if(mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $data = $row['column_name']; // Change 'column_name' to the actual column name you want to display
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Display Database Query Results</title>
</head>
<body>
    <textarea><?php echo $data; ?></textarea>
</body>
</html>