About how does mysqli_query() Function and mysqli_fetch_array($result) work?

As already said it is all in the manuals, you should read these sites: http://php.net/manual/en/mysqli.query.php, http://php.net/manual/en/mysqli-result.fetch-array.php and http://php.net/manual/en/mysqli-result.fetch-assoc.php

In general you start doing the mysqli_query which may return a result object.

Then you use the result with mysqli_fetch_array or mysqli_fetch_assoc (I personally prefer assoc) to get the rows. You get one row each time you run the fetch function.

I copy example 1 from the php.net link:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");

/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$query = "SELECT Name, CountryCode FROM City ORDER by ID LIMIT 3";
$result = $mysqli->query($query);

/* numeric array */
$row = $result->fetch_array(MYSQLI_NUM);
printf ("%s (%s)\n", $row[0], $row[1]);

/* associative array */
$row = $result->fetch_array(MYSQLI_ASSOC);
printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);

/* associative and numeric array */
$row = $result->fetch_array(MYSQLI_BOTH);
printf ("%s (%s)\n", $row[0], $row["CountryCode"]);

/* free result set */
$result->free();

/* close connection */
$mysqli->close();
?>

Leave a Comment