How can the error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" be resolved when fetching data from a MySQL query in PHP?

The error "Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource" occurs when the result of a MySQL query is not successfully returned. This can happen due to errors in the query itself or connection issues. To resolve this issue, ensure that the query is executed correctly and that the connection to the MySQL database is established.

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

// Execute query
$query = "SELECT * FROM table_name";
$result = mysqli_query($connection, $query);

// Check if query executed successfully
if($result) {
    // Fetch data from query result
    while($row = mysqli_fetch_array($result)) {
        // Process data
    }
} else {
    echo "Error: " . mysqli_error($connection);
}

// Close connection
mysqli_close($connection);