Database Reference
In-Depth Information
To determine metadata availability, check whether the statement handle colum
nCount() method returns a value greater than zero. If so, the handle's getColumnMe
ta() method returns an associative array containing metadata for a single column. The
following table shows the elements of this array. (The format of the flags value might
differ for other database systems.)
Name
Value
Column type (corresponds to a PDO::PARAM_ XXX value)
pdo_type
PHP native type for the column value
native_type
Column name
name
Column length
len
Column precision
precision
Array of flags describing the column attributes
flags
Name of table the column is part of
table
This example code shows how to execute a statement and display result set metadata:
$stmt = "SELECT name, birth FROM profile" ;
print ( "Statement: $stmt \n " );
$sth = $dbh -> prepare ( $stmt );
$sth -> execute ();
# metadata information becomes available at this point ...
$ncols = $sth -> columnCount ();
print ( "Number of columns: $ncols \n " );
if ( $ncols == 0 )
print ( "Note: statement has no result set \n " );
for ( $i = 0 ; $i < $ncols ; $i ++ )
{
$col_info = $sth -> getColumnMeta ( $i );
$flags = implode ( "," , array_values ( $col_info [ "flags" ]));
printf ( "--- Column %d (%s) --- \n " , $i , $col_info [ "name" ]);
printf ( "pdo_type: %d \n " , $col_info [ "pdo_type" ]);
printf ( "native_type: %s \n " , $col_info [ "native_type" ]);
printf ( "len: %d \n " , $col_info [ "len" ]);
printf ( "precision: %d \n " , $col_info [ "precision" ]);
printf ( "flags: %s \n " , $flags );
printf ( "table: %s \n " , $col_info [ "table" ]);
}
The program produces this output:
Statement: SELECT name, birth FROM profile
Number of columns: 2
--- Column 0 (name) ---
PDO type: 2
native type: VAR_STRING
len: 20
precision: 0
flags: not_null
 
Search WWH ::




Custom Search