Why are SQL queries not arrays?


I’ve been using mysqli queries recently, but their use isn’t very intuitive for me. For example, let’s say I have a table with three entries:

Table A
-Column 1

If I did

$query = $mysqli->query("SELECT Column1 FROM Table A);

I would expect that $query would hold the values for that column, but I have to follow it with something like

while ($value = $query->fetch_row()){
echo $value;

I just would like to know an example of when you would want to use a query result as a query result, and not just to access the values that it returns. Sorry If I just ask this to get a better grasp of the language, but I find that understanding the paradagim of a language really helps in using it better.


Do yourself a favor and learn PDO instead of Mysqli. It’s what the “Big Boys” use. Here is a tutorial to get you going. Come on back if you get stuck.


99% of the queries people write will return multiple records so by default ->query returns a resource that allows you to while-loop over all of the records returned, even if it’s only 1.

I tend to have a helper function that does something like…

$all = array();
while( $record = $query->fetch_row() ){
    $all[] = $record;
return $all;

… so that I can work with an array in a foreach loop instead of a resource in a while loop.