How can the issue of only retrieving the first element of an array from a database query in PHP be resolved?

Issue: When retrieving data from a database query in PHP, if the result is an array and we only want to access the first element, we can use the mysqli_fetch_assoc() function to fetch the first row as an associative array and then access the desired element using its key.

// Assuming $result is the result of a database query
$row = mysqli_fetch_assoc($result);
$firstElement = $row['desired_key'];