array tableInfo (
mixed $result
,
integer $mode
= null
)
Get information about columns in a table or a query result.
$result
DB_result object from a query or a string containing the name of a table
If the name of the table needs to be delimited (ie: the name is a reserved word or has spaces in it), use the quoteIdentifier() method on the table name when passing it.
This can also be a query result resource identifier, but doing so is deprecated.
$mode
one of the tableInfo mode constants
array - an associative array of the table's information or a DB_Error object on failure
The array's contents depends on the $mode
parameter.
The names of tables and columns will be lowercased if the DB_PORTABILITY_LOWERCASE portability mode is enabled.
The flags
element contains a space
separated list of extra information about the column.
If the DBMS is able to report a column's default value,
the value is passed through rawurlencode()
to avoid problems caused by potential spaces in the value.
Most DBMS's only provide the table
and
flags
elements if $result
is a table name. Only fbsql and mysql provide full
information from queries.
The type
element contains the type returned
by the DBMS. It varies from DBMS to DBMS.
This section describes the format of the returned array and how
it varies depending on which $mode
was used
when the function was called.
The sample output below is based on this query:
SELECT tblFoo.fldID, tblFoo.fldPhone, tblBar.fldId FROM tblFoo JOIN tblBar ON tblFoo.fldId = tblBar.fldId;
0
In addition to the information found in the default output,
a notation of the number of columns is provided by the
num_fields
element while the
order
element provides an array with the column names as the keys and
their location index number (corresponding to the keys in the
default output) as the values.
If a result set has identical field names, the last one is used.
Similar to DB_TABLEINFO_ORDER but adds more dimensions to the array in which the table names are keys and the field names are sub-keys. This is helpful for queries that join tables which have identical field names.
Contains the information from both DB_TABLEINFO_ORDER and DB_TABLEINFO_ORDERTABLE
The tableInfo mode constants are bitwised, so they can be
combined using |
.
Error code | Error message | Reason | Solution |
---|---|---|---|
DB_ERROR_NOT_CAPABLE | DB backend not capable | Driver doesn't support this feature. | Switch to another database system, if you really need this feature. |
DB_ERROR_NEED_MORE_DATA | insufficient data supplied |
The data passed in the $result parameter
was not a valid table name or result identifier.
|
Check the table name for typographical errors or that the query ran correctly. |
DB_ERROR_NODBSELECTED | no database selected | No database was chosen. | Check the DSN in connect(). |
can't distinguish duplicate field names | The query result has multiple columns with the same name. PHP's Informix extension deals with columns having the same names by overwriting the prior columns information. Therefore, tableInfo() is unable to properly represent these result sets. | Use aliases for columns that have the same names. |
This function can not be called statically.
tableInfo() is not portable because not all drivers have this method, many DBMS's are unable to determine table names from query results and the metadata returned by each database system varies dramatically.
Finding information about a table
<?php
// Once you have a valid DB object named $db...
$info = $db->tableInfo('tablename');
print_r($info);
?>
Finding information about a query result
<?php
// Once you have a valid DB object named $db...
$res =& $db->query('SELECT * FROM tablename');
$info = $db->tableInfo($res);
print_r($info);
?>
Prior to version 1.6.0, tableInfo() was a part of the DB_result class, so had to be called like this:
<?php
// Once you have a valid DB object named $db...
$res =& $db->query('SELECT * FROM tablename');
$info = $res->tableInfo(); // <---- DEPRECATED
print_r($info);
?>