In Lesson 2, the concept of the ActualTypes was explained. In this section, we show how the value of a field, as well as its attributes, can be obtained. By treating the returned result set as a 2 dimensional array, we can process each row on a column-by-column basis.
ADOdb provides 2 methods that shows the boundaries of the returned data:
We can then move across rows and columns until we reach the limits of each boundary.
In Lesson 1, we saw a number of ways of moving from one row to the next. It is important to not mix methods, as some methods read the record and automatically advance the record pointer, and some do not. However in all cases, a flag EOF is set as soon as the end of the result set is reached. In the following example, the method moveNext() is used.
Note the for both rows and columns, ADOdb uses the PHP convention of a Zero-Based (0) array, as opposed to SQL offsets, which are One-based (1).
include 'adodb/adodb.inc.php'; $sql = 'select * from some_table'; $recordSet = $db->execute($sql);
Once the query has been executed, we can query both the row and column (field) count.
$rows = $recordSet->rowCount(); $columns = $recordSet->fieldCount();
For this loop, we are going to rely on the End-Of-File marker, but we could just as easily loop until $rows
is reached. Note also that both the execute()
and selectLimit()
methods position the cursor at the first row of the result set.
while (!$recordSet->EOF) {
We are now going to loop through each column of each row and use 3 methods to ascertain the value and attributes of the field:
for ($colIndex=0;$colIndex<$columns;$colIndex++) { /* * This is the attribute of the field, returned as an * adoFieldObject by fetchField() */ $fieldInfo = $recordSet->fetchField($colIndex); $type = $recordSet->metaType($fieldInfo->type); /* * This is the value of the field */ $value = $recordSet->fields($colIndex);
At this point in the loop, we make a formatting decision based on the portable metaType attribute of the column. If the type of the field is a timestamp, we will immediately format the data to present the data as a human readable date
if ($type == 'T') $value = date('m/d/Y',$value); if ($colIndex == 0) print PHP_EOL; print $value . "\t"; } /* * End of the colums, move to next row */ $recordSet->moveNext(); }
End Of Lesson