This is an old revision of the document!
Fields And Fields Objects
Formatting Of Returned Data
ADOdb provides a number of methods for controlling returned data. The data may be represented as arrays, both numeric and associative, and as objects.
In addition to returning recordsets on a row by row basis, ADOdb allows the user to interrogate each row on a field by field basis.
/* * Load the common code */ include 'adodb.inc.php'; may while (!$recordSet->EOF) { $fld = $recordSet->FetchField(1); $type = $recordSet->MetaType($fld->type); if ( $type == 'D' || $type == 'T') print $recordSet->fields[0] .' ' . $recordSet->UserDate($recordSet->fields[1],'m/d/Y') .'<BR>'; else print $recordSet->fields[0].' '.$recordSet->fields[1].'<BR>'; $recordSet->MoveNext(); } $recordSet->Close(); # optional $conn->Close(); # optional
In this example, we check the field type of the second column using fetchField(). This returns an adoFieldObject object with at least 3 fields.
name: name of column type: native field type of column max_length: maximum length of field.
Some databases such as MySQL do not return the maximum length of the field correctly. In these cases max_length will be set to -1. We then use metaType() to translate the native type to a generic type. Currently the following generic types are defined:
C: character fields that should be shown in a <input type="text"> tag. X: TeXt, large text fields that should be shown in a <textarea> B: Blobs, or Binary Large Objects. Typically images. D: Date field T: Timestamp field L: Logical field (boolean or bit-field) I: Integer field N: Numeric field. Includes autoincrement, numeric, floating point, real and integer. R: Serial field. Includes serial, autoincrement integers. This works for selected databases.
If the metatype is of type date or timestamp, then we print it using the user defined date format with UserDate(), which converts the PHP SQL date string format to a user defined one.
Another use for metaType()
is data validation before doing an SQL insert or update.
Usage
/* * DB Connection assumed */ $result = $db->Execute("SELECT * FROM ACT"); $r = $result->FetchRow(); print $r->fields(1); /* * Returns: 10 */