How can the PHP function mysqli_fetch_array be used effectively to retrieve data from a database?

To effectively retrieve data from a database using the mysqli_fetch_array function in PHP, you need to first establish a connection to the database using the mysqli_connect function. Then, you can execute a query using mysqli_query and pass the result to mysqli_fetch_array to fetch a row of data as an associative array. This function can be used in a loop to retrieve multiple rows of data from the database.

// Establish a connection to the database
$conn = mysqli_connect("localhost", "username", "password", "database");

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

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

// Fetch data using mysqli_fetch_array
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    // Process data
    echo $row['column1'] . " " . $row['column2'] . "<br>";
}

// Close connection
mysqli_close($conn);