ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:userguide:learn_fields:field_objects

Fields As Objects

In Lesson 2, the concept of the ActualTypes was explained. In this section, we show how the value of field, and also the attributes of the same field 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:

  • rowCount(), which returns the number of rows in the returned resultset.
  • 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 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:

  • fields(), which returns the value of a field at a specified offset.
  • fetchField(), which returns an adoFieldObject object, containing schema information about the field.
  • 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($fld->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

v5/userguide/learn_fields/field_objects.txt · Last modified: 2020/01/02 12:03 by dregad