DB_common::getAssoc()

DB_common::getAssoc() -- Runs a query and returns the data as an array

Synopsis

array &getAssoc (string $query [, boolean $force_array = FALSE [, mixed $params = array() [, integer $fetchmode = DB_FETCHMODE_DEFAULT [, boolean $group = FALSE]]]])

ïÐÉÓÁÎÉÅ

Runs the query provided and puts the entire result set into an associative array then frees the result set.

If the result set contains more than two columns, the value will be an array of the values from column 2 to n. If the result set contains only two columns, the returned value will be a scalar with the value of the second column (unless forced to an array with the $force_array parameter).

ðÁÒÁÍÅÔÒ

string $query

the SQL query or the statement to prepare

boolean $force_array

used only if the query returns exactly two columns. If TRUE, the values of the returned array will be one-element arrays instead of scalars.

mixed $params

array, string or numeric data to be added to the prepared statement. Quantity of items passed must match quantity of placeholders in the prepared statement: meaning 1 placeholder for non-array parameters or 1 placeholder per array element.

If supplied, prepare()/ execute() is used.

integer $fetchmode

the fetch mode to use. The default is DB_FETCHMODE_DEFAULT, which tells this method to use DB's current fetch mode. DB's current default fetch mode can be changed using setFetchMode(). Potential values include:

  • DB_FETCHMODE_ORDERED

  • DB_FETCHMODE_ASSOC

  • DB_FETCHMODE_OBJECT

boolean $group

if TRUE, the values of the returned array is wrapped in another array. If the same key value (in the first column) repeats itself, the values will be appended to this array instead of overwriting the existing values.

÷ÏÚ×ÒÁÝÁÅÍÏÅ ÚÎÁÞÅÎÉÅ

array - associative array with the query results or a DB_Error object on failure

Throws

Таблица 34-1. ÷ÏÚÍÏÖÎÙÅ ÚÎÁÞÅÎÉÑ PEAR_Error

Error codeError messageReasonSolution
DB_ERROR_INVALID invalid SQL statment for preparing is not valid. See the prepare() documentation, if you want to use a SQL statemt using placeholders.
DB_ERROR_MISMATCH mismatch Quantity of parameters didn't match quantity of placeholders in the prepared statment. Check that the number of placeholders in the prepare() statement passed to $query equals the count of entries passed to $params.
DB_ERROR_NODBSELECTED no database selected No database was choosen. Check the DSN in connect().
DB_ERROR_TRUNCATED truncated The result set contains fewer then two columns Check the SQL query or choose another get*() function
every other error code  Database specific error Check the database related section of PHP-Manual to detect the reason for this error. In the most cases a misformed SQL statment. Ie. using LIMIT in a SQL-Statment for an Oracle database.

úÁÍÅÔËÁ

üÔÁ ÆÕÎËÃÉÑ ÎÅ ÄÏÌÖÎÁ ×ÙÚÙ×ÁÔØÓÑ ÓÔÁÔÉÞÅÓËÉ.

ðÒÉÍÅÒ

All of the examples use the following data set:
INSERT INTO foo VALUES ('Juan', 5, '1991-01-11 21:31:41');
INSERT INTO foo VALUES ('Kyu', 10, '1992-02-12 22:32:42');
INSERT INTO foo VALUES ('Kyu', 15, '1993-03-13 23:33:43');

Result sets having two columns

When using getAssoc() for results which have two columns and $force_array = FALSE (the default) changing $fetchmode has no impact on the format of the resulting array.

Result sets having more than two columns

Prepare / Execute Mode

óÍ. ÔÁËÖÅ

setFetchMode(), getOne(), getRow(), getCol(), getAll(), query(), "Intro - Prepare & Execute"


HIVE: All information for read only. Please respect copyright!
Hosted by hive ÊÃÁ: Êèåâñêàÿ ãîðîäñêàÿ áèáëèîòåêà