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

Both sides previous revisionPrevious revision
v5:userguide:learn_fields:start_lesson [2016/03/17 04:22] mnewnhamv5:userguide:learn_fields:start_lesson [2016/03/18 00:32] (current) mnewnham
Line 1: Line 1:
-<- v5:userguide:learn_fields:introduction|Introduction ^ v5:userguide:learn_fields:start_lesson|Start Of Lesson ^ v5:userguide:learn_fields:| ->+<- v5:userguide:learn_fields:introduction|Introduction ^ v5:userguide:learn_fields:start_lesson|Start Of Lesson ^ v5:userguide:learn_fields:associative_key_case|Associative Key Case ->
 ~~NOTOC~~ ~~NOTOC~~
 ====== Fields And Fields Objects ====== ====== Fields And Fields Objects ======
Line 5: Line 5:
 ADOdb provides a number of methods for controlling returned data. The data may be represented as arrays, both numeric and associative, and as objects. ADOdb provides a number of methods for controlling returned data. The data may be represented as arrays, both numeric and associative, and as objects.
  
 +===== $ADODB_FETCH_MODE =====
 +The global variable [[v5:reference:adodb_fetch_mode|$ADODB_FETCH_MODE]] controls how the keys of the arrays containing the returned data are presented. The variable should be set **//before//** the query is executed. In addition to the variable, the constants **ADODB_FETCH_NUM,ADODB_FETCH_ASSOC,ADODB_FETCH_BOTH and ADODB_FETCH_NATIVE** are available to help. The available values are described as follows:
 +^Name^Value^Description^
 +|ADODB_FETCH_DEFAULT|0|The recordset is returned in the default provided by the PHP driver. Use of this value is not recommended if writing cross-database applications|  
 +|ADODB_FETCH_NUM|1|The recordset is returned as a numeric array|
 +|ADODB_FETCH_ASSOC|2|The recordset is returned as an associative array|
 +|ADODB_FETCH_BOTH|3|The record is returned as both a numeric and associative arrays. Not supported in some databases|
  
-In addition to returning recordsets on a row by row basisADOdb allows the user to interrogate each row on a field by field basis.  +  $ADODB_FETCH_MODE = ADODB_FETCH_NUM; 
 +returns: 
 +  array([0] =>'v0', 
 +        [1] =>'v1', 
 +        [2] =>'v2', 
 +        etc.... 
 +  )
  
-<code php> +  $ADODB_FETCH_MODE = ADODB_FETCH_ASSOC;   
-/* +returns: 
-* Load the common code +  array(['col0'] =>'v0', 
-*/ +        ['col1'] =>'v1', 
-include 'adodb.inc.php';+        ['col2'] => 'v2', 
 +        etc..... 
 + )
  
-may  +  $ADODB_FETCH_MODE = ADODB_FETCH_BOTH;   
-while (!$recordSet->EOF{+returns: 
 +  array([0] = 'v0', 
 +        ['col0'] =>'v0', 
 +        [1] = 'v1', 
 +        ['col1'] =>'v1', 
 +        [2] => 'v2', 
 +        ['col2'] => 'v2', 
 +        etc..... 
 + ) 
 + 
  
-     $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