Fetch the entire result set of a query and return it as an associative array using the first column as the key.
Note: column names are not preserved when using this function.
For example, if the table 'mytable' contains:
ID TEXT DATE -------------------------------- 1 'one' 944679408 2 'two' 944679408 3 'three' 944679408
$q = new Query("SELECT id, text FROM mytable") $q->getAssoc() returns: array( '1' => array('one'), '2' => array('two'), '3' => array('three'), )
... or call $q->getAssoc($scalar=true) to avoid wrapping results in an array (only applies if only 2 cols are returned): array( '1' => 'one', '2' => 'two', '3' => 'three', ) Keep in mind that database functions in PHP usually return string values for results regardless of the database's internal type.
Parameters |
| boolean | $scalar Used only when the query returns exactly two columns. If TRUE, the values of second column are not wrapped in an array. Default here is false, in order to assure expected behavior. |
|
Returns |
array Associative array with results from the query. |
|