How can data from a database be outputted in a text field in PHP?

To output data from a database in a text field in PHP, you can retrieve the data from the database using SQL queries and then echo the data within the value attribute of the text field in your HTML form. Make sure to properly sanitize and escape the data to prevent SQL injection attacks.

<?php
// Connect to your database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Retrieve data from the database
$query = "SELECT column_name FROM table_name WHERE condition";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);
$data = $row['column_name'];

// Output the data in a text field
echo '<input type="text" name="textfield" value="' . htmlspecialchars($data) . '">';
?>