How can the while loop in the PHP code be optimized to display only one data record from the database?

The issue can be solved by modifying the while loop to fetch only one data record from the database. This can be achieved by using the "LIMIT 1" clause in the SQL query to retrieve only the first record. By doing this, the while loop will iterate only once, displaying the single data record fetched from the database.

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

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

// SQL query to fetch one data record
$sql = "SELECT * FROM table_name LIMIT 1";
$result = mysqli_query($connection, $sql);

// Display data record
if (mysqli_num_rows($result) > 0) {
    $row = mysqli_fetch_assoc($result);
    echo "Name: " . $row["name"] . "<br>";
    echo "Age: " . $row["age"] . "<br>";
    // Display other fields as needed
} else {
    echo "No records found";
}

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