<- v5:userguide:learn_fields:associative_key_case|ADODB_ASSOC_CASE ^ v5:userguide:learn_fields:start_lesson|Start Of Lesson ^ -> ~~NOTOC~~ ====== Fields As Objects ====== 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. ===== The Limits Of The Array ===== ADOdb provides 2 methods that shows the boundaries of the returned data: * [[v5:reference:connection:recordcount|rowCount()]], which returns the number of rows in the returned resultset. * [[v5:reference:recordset:fieldcount|fieldCount()]]. which returns the number of columns in the returned resultset. Note that this value never varies between rows. We can then move across rows and columns until we reach the limits of each boundary. ===== Walking The Rows Of A ResultSet ===== 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 [[v5:reference:recordset:movenext|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) { ===== Interrogating The Columns ===== 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: * [[v5:reference:recordset:fields|fields()]], which returns the value of a field at a specified offset. * [[v5:reference:recordset:fetchfield|fetchField()]], which returns an [[v5:dictionary:adofieldobject|adoFieldObject]] object, containing schema information about the field. * [[v5:dictionary:metatype|metaType()]], which converts the database specific information in the adoFieldObject to a portable metaType 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); ===== MetaType Based Decisions ===== 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**