mysqli_fetch_array returning only one result
I'm trying to make a very, very simple query of a small mysql database, using the following code (with appropriate values in $host, etc.):
$result = mysqli_query($connection, "select university from universities_alpha");
$row = mysqli_fetch_array($result);
echo print_r($result);
echo '<br><br>';
echo print_r($row);
As you can see, I printed out the results in a human-readable way, yielding:
mysqli_result Object ( [current_field] => 0 [field_count] => 1 [lengths] => Array ( [0] => 19 ) [num_rows] => 9 [type] => 0 ) 1
Array ( [0] => Arizona State Univ. [university] => Arizona State Univ. ) 1
There are a few example universities in that column, so I'm not sure what I'm doing wrong.
mysqli_fetch_array works by pointers each time it's called
Imagine the following
$result = mysqli_query($connection, "select university from universities_alpha");
$row = mysqli_fetch_array($result); // this is the first row
$row = mysqli_fetch_array($result); // now it's the second row
$row = mysqli_fetch_array($result); // third row
To actually display the data the way you want it to, I suggest you do the following
$rows = array();
$result = mysqli_query($connection, "select university from universities_alpha");
while($row = mysqli_fetch_array($result)) {
$rows[] = $row;
}
print_r($rows);
If you want to fetch all rows from the result set then you must use mysqli_fetch_all()
instead of mysqli_fetch_array()
.
Replace your code with this:
$result = mysqli_query($connection, "select university from universities_alpha");
$rows = mysqli_fetch_all($result, MYSQLI_BOTH);