DB_common::getAssoc()

DB_common::getAssoc()

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

Description

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).

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.

Return value

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

Note

This function can not be called statically.

Example

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

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.