ADOdb Library for PHP Manual
Prev Advanced Select with Field Objects Next

Advanced Select with Field Objects

Select a table, display the first two columns. If the second column is a date or timestamp, reformat the date to US format.

<?php
    
include('adodb.inc.php');       # load code common to ADOdb
    
$conn = &ADONewConnection('access');    # create a connection
    
$conn->PConnect('northwind');   # connect to MS-Access, northwind dsn
    
$recordSet = &$conn->Execute('select CustomerID,OrderDate from Orders');
    if (!
$recordSet
        print 
$conn->ErrorMsg();
    else
    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 object with at least 3 fields.

We then use MetaType() to translate the native type to a generic type. Currently the following generic types are defined:

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.


Prev Home Next
Select Statement Up Inserting

Sponsored by phpLens