How can a while loop be used to display MySQL data in a text field in PHP?

To display MySQL data in a text field in PHP using a while loop, you can fetch the data from the database using a SELECT query, then loop through the results using a while loop to populate the text field with the data.

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

// Check connection
if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

// Fetch data from MySQL
$sql = "SELECT column_name FROM table_name";
$result = mysqli_query($connection, $sql);

// Display data in a text field using a while loop
echo '<input type="text" name="textfield" value="">';
while ($row = mysqli_fetch_assoc($result)) {
    echo '<input type="text" name="textfield" value="' . $row['column_name'] . '">';
}

// Close MySQL connection
mysqli_close($connection);
?>