About the fetch() and fetchAll() methods
As you’ve seen, the fetch() method returns each row in the result set, one after another, or FALSE if there are now rows returned.
On the other hand fetchAll() method returns an array containing all the rows in the result set.
Either way, the methods return an array representing each column if we use fetch(), or an array of rows with each element of the array (representing each row) containing an array of column values.
As you saw in almost all my examples, I am using the FETCH_ASSOC constant when i retrieve results from a database. That is because is easier for me to use the values from the database by naming the their respectiv columns.
So, when you retrieve the result, you usually call the value of the result row as follows: $row[‘column’].
Is a constant that returns an array indexed by column number as returned in your result set, starting with element (column) 0.
That means that you will have to call the first column of the row as follows: $row.
Being a memory consumer, the constant FETCH_BOTH will return an array indexed by both column name and number-indexed column number. That means that you can call the columns either by $row[‘firstcolumn’] or $row.
This constant will return you an object with property names that correspond to the column names returned in the result set. You will therefore have to get the column by calling it like so: $row->firstcolumn.
For this lesson, I think we’ve learned enough. There are a few more things to say, but I think that for the basics is enough. If you think I missed something, please write me a comment. Thank you