Results
Описание
Fetching Individual Rows From Query Results
The MDB2_Result_Common object provides two methods for fetching data from rows of a result set: fetchOne(), fetchRow(), fetchCol() and fetchAll().
fetchRow() and fetchOne() read an entire row or a single field from a column respectively. The result pointer gets moved to the next row each time these methods are called. NULL is returned when the end of the result set is reached.
fetchAll() and fetchCol() read all rows in the result set and therefore move the result pointer to the end. While fetchAll() reads the entire row data, fetchCol() only reads a single column.
MDB2_Error is returned if an error is encountered.
Пример 35-1. Fetching a result set
|
Formats of Fetched Rows
The data from the row of a query result can be placed into one of three constructs: an ordered array (with column numbers as keys), an associative array (with column names as keys) or an object (with column names as properties).
MDB2_FETCHMODE_ORDERED (default)
Array ( [0] => 28 [1] => hi ) |
MDB2_FETCHMODE_ASSOC
Array ( [a] => 28 [b] => hi ) |
MDB2_FETCHMODE_OBJECT
stdClass Object ( [a] => 28 [b] => hi ) |
NOTE: When a query contains the same column name more than once (such as when joining tables which have duplicate column names) and the fetch mode is MDB2_FETCHMODE_ASSOC or MDB2_FETCHMODE_OBJECT, the data from the last column with a given name will be the one returned. There are two immediate options to work around this issue:
Use aliases in your query, for example People.Name AS PersonName |
Change the fetch mode to MDB2_FETCHMODE_ORDERED |
TIP: If you are running into this issue, it likely indicates poor planning of the database schema. Either data is needlessly being duplicated or the same names are being used for different kinds of data.
How to Set Formats
You can set the fetch mode each time you call a fetch method and/or you can set the default fetch mode for the whole MDB2 instance by using the setFetchMode() method.
Пример 35-2. Determining fetch mode per call
|
Пример 35-3. Changing default fetch mode
|
Fetch Rows by Number
The PEAR MDB2 fetch system also supports an extra parameter to the fetch statement. So you can fetch rows from a result by number. This is especially helpful if you only want to show sets of an entire result (for example in building paginated HTML lists), fetch rows in an special order, etc.
Пример 35-4. Fetching by number
|
Getting Entire Result Sets
The MDB2_Result_Common object provides several methods to read entire results sets: fetchCol() and fetchAll().
Freeing Result Sets
Once you finish using a result set, if your script continues for a while, it's a good idea to save memory by freeing the result set via Use free().
Пример 35-5. Freeing
|
Getting the native result resource
If whatever data you need to read from a result set is not yet implemented in MDB2 you can get the native result resource using the getResource() method and then call the underlying PHP extension directly (though this would of course require that it is now up to you to make this sufficiently portable).
Пример 35-6. Native result resource
|
Getting More Information From Query Results
With MDB2 there are four ways to retrieve useful information about the query result sets themselves:
Пример 35-7. numRows() tells how many rows are in a SELECT query result
|
Пример 35-8. numCols() tells how many columns are in a SELECT query result
|
Пример 35-9. rowCount() tells which row number the internal result row pointer currently points to
|
Пример 35-10. getColumnNames() returns an associative array with the names of the column of the result set as keys and the position inside the result set as the values
|
Пример 35-11. seek() allows to seek to a specific row inside a result set. Note that seeking to previously read rows is only possible if the 'result_buffering' option is left enabled, otherwise only forward seeking is supported.
|
Пример 35-12. nextResult() allows iterate over multiple results returned by a multi query.
|
Пример 35-13. bindColumn() allows to bind a reference to a user variable to a specific field inside the result set. This means that when fetching the next row, this variable is automatically updated.
|
Querying and fetching in one call
All of the fetch methods are also available in a variant that executes a query directly: queryOne(), queryRow(), queryCol() and queryAll().
Пример 35-14.
|
Users that prefer to use prepared statements can make use of the following methods from the Extended module: getOne(), getRow(), getCol(), getAll() and getAssoc().
Пример 35-15.
|
Data types
MDB2 supports a number of data types across all drivers. These can be set for result sets at query or prepare time or using the setResultTypes() method. You can find an overview of the supported data types and their format here.
Fetching LOBs
To retrieve a Large Object (BLOB or CLOB), you can use streams, as you were reading a file
Пример 35-16. Fetching LOBs with streams.
|
Checking for Errors
Don't forget to use isError() to check if your actions return a MDB2_Error object.
Пред. | Начало | След. |
Datatypes | Уровень выше | Prepare & Execute |