What role does the mysql_fetch_array function play in PHP MySQL queries, and how does it differ from iterating through results with a loop?

The mysql_fetch_array function in PHP is used to retrieve a single row from a MySQL result set as an associative array or a numeric array. It differs from iterating through results with a loop as it fetches only one row at a time, while a loop can be used to iterate through multiple rows in the result set.

// Connect to MySQL database
$conn = mysqli_connect("localhost", "username", "password", "database");

// Execute query
$result = mysqli_query($conn, "SELECT * FROM table");

// Fetch and display results using mysql_fetch_array
while ($row = mysqli_fetch_array($result)) {
    echo $row['column_name'] . "<br>";
}

// Close connection
mysqli_close($conn);