What is the process for retrieving data from a MySQL database and displaying it in form fields using PHP?

To retrieve data from a MySQL database and display it in form fields using PHP, you need to establish a connection to the database, execute a query to retrieve the data, and then populate the form fields with the retrieved data.

<?php
// Establish a connection to the MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database_name");

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

// Retrieve data from the database
$query = "SELECT * FROM table_name WHERE id = 1";
$result = mysqli_query($connection, $query);
$row = mysqli_fetch_assoc($result);

// Populate form fields with retrieved data
echo '<input type="text" name="field1" value="' . $row['field1'] . '">';
echo '<input type="text" name="field2" value="' . $row['field2'] . '">';
echo '<input type="text" name="field3" value="' . $row['field3'] . '">';

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