Are there more efficient ways to populate form fields with data from a MySQL database in PHP?

When populating form fields with data from a MySQL database in PHP, one efficient way to do so is by fetching the data from the database and then using that data to populate the form fields. This can be achieved by querying the database for the specific data needed, storing it in variables, and then echoing those variables into the form fields.

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

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

// Query to fetch data from database
$query = "SELECT * FROM table_name WHERE id = 1";
$result = mysqli_query($connection, $query);

// Fetch data and populate form fields
if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    $field1 = $row['field1'];
    $field2 = $row['field2'];
    $field3 = $row['field3'];
}

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

<form>
    <input type="text" name="field1" value="<?php echo $field1; ?>">
    <input type="text" name="field2" value="<?php echo $field2; ?>">
    <input type="text" name="field3" value="<?php echo $field3; ?>">
</form>