ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:reference:connection:getassoc

This is an old revision of the document!


getAssoc

syntax
 mixed getAssoc (
       string $sql
       optional string[] $bindvars=false
       optional bool $forceArray=false
       optional bool $first2Cols=false
       )

Description

The function executes a statement and returns all of the returned rows in an array or false if the statement execution fails, or if only 1 column is requested in the SQL statement. If no records match the provided SQL statement, an empty array is returned.

The presentation of the returned data can be modified by the variable $ADODB_ASSOC_CASE.

For a detailed description of the SQL and bind parameters passed, see execute().

In addition it should be noted that the presentation of the returned data is modified by the number of columns requested in the SQL statement and the setting of the optional parameters $forceArray and $first2Cols.

Requesting two columns

When 2 columns are requested, the first column becomes the key and the second the value.

/*
 * DB2 Connection assumed
*/
$ar = $db->getAssoc("SELECT ACTNO,ACTDESC FROM ACT");
 
/*
 * $ar returns:
Array
(
    [10] => MANAGE/ADVISE
    [20] => ESTIMATE COST
    [30] => DEFINE SPECS
    [40] => LEAD PROGRAM/DESIGN
 ......
)
*/
 

Requesting more than two columns

When more than 2 columns are requested, the first column becomes the key, all other columns become a numeric array beneath that key.

/*
 * DB2 Connection assumed
*/
$ar = $db->getAssoc("SELECT * FROM ACT");
 
/*
 * $ar returns:
Array
(
    [10] => Array
        (
            [0] => MANAGE
            [1] => MANAGE/ADVISE
        )
 
    [20] => Array
        (
            [0] => ECOST
            [1] => ESTIMATE COST
        )
 
    [30] => Array
        (
            [0] => DEFINE
            [1] => DEFINE SPECS
        )
 
 ......
)
*/
 

Requesting two columns and setting the $forceArray flag

When 2 columns are requested and the $forceArray boolean is set, the first column becomes the key and the second becomes an array beneath that key.

/*
 * DB2 Connection assumed
*/
$ar = $db->getAssoc("SELECT ACTNO,ACTDESC FROM ACT",false,true);
 
/*
 * $ar returns:
   Array
(
    [10] => Array
        (
            [0] => MANAGE/ADVISE
        )
 
    [20] => Array
        (
            [0] => ESTIMATE COST
        )
 
    [30] => Array
        (
            [0] => DEFINE SPECS
        )
 ......
)
*/
 

Requesting more than two columns and setting the $first2Cols flag

When more than 2 columns are requested, and the $first2cols flag is set, the behaviour is the same as the default configuration. The $forceArray flag is ignored. All columns after the first two are discarded.

/*
 * DB2 Connection assumed
*/
$ar = $db->getAssoc("SELECT * FROM ACT",false,false,true);
 
/*
 * $ar returns:
Array
(
    [10] => MANAGE/ADVISE
    [20] => ESTIMATE COST
    [30] => DEFINE SPECS
    [40] => LEAD PROGRAM/DESIGN
 ......
)
*/
 

Using The Recordset Method

The method can also be used as a recordset method

$ar = $db->Execute("SELECT * FROM ACT")->getAssoc();
v5/reference/connection/getassoc.1456276508.txt.gz · Last modified: 2017/04/21 11:35 (external edit)