ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:userguide:learn_fields:start_lesson

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Last revisionBoth sides next revision
v5:userguide:learn_fields:start_lesson [2016/03/17 01:40] – created mnewnhamv5:userguide:learn_fields:start_lesson [2016/03/17 04:22] mnewnham
Line 2: Line 2:
 ~~NOTOC~~ ~~NOTOC~~
 ====== Fields And Fields Objects ====== ====== 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.  
 +
 +<code php>
 +/*
 +* 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
 +</code>
 +
 +In this example, we check the field type of the second column using [[v5:reference:recordset:fetchfield|fetchField()]]. This returns an [[v5:dictionary: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 [[v5:dictionary:metatype|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 [[v5:reference:connection:userdate|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 =====
 +<code php>
 +/*
 + * DB Connection assumed
 + */
 +$result = $db->Execute("SELECT * FROM ACT");
 +$r = $result->FetchRow();
 +print $r->fields(1);
 +/*
 + * Returns: 10
 + */
 +</code>
v5/userguide/learn_fields/start_lesson.txt · Last modified: 2016/03/18 00:32 by mnewnham