- skip to content

# Database Abstraction Layer

### User Tools

- Login

### Site Tools

- Recent changes
- Media Manager
- Sitemap

### Sidebar

##### Resources

- Reference Manual
- User Guide
- The Data Dictionary
- ADOdb-xmlschema
- ADOdb Active Record
- Performance Monitoring
- Date/Time Library
- Session Management Library
- Unsupported Addons
- Coding Standards

##### Supported Databases

- Access
- ADO
- CSV
- Firebird
- Frontbase
- IBM DB2
- Informix
- Interbase
- LDAP
- Microsoft SQL Server
- MySQL
- Netezza
- ODBC
- ODBTP
- Oracle
- PDO
- Postgresql
- SAP ADS
- SAP ASE
- SAP Maxdb
- SAP Sqlanywhere
- Sqlite
- Sybase
- Plain Text
- Visual Foxpro

##### Other

- Feature Comparison Matrix
- Connection Matrix
- Include File Guide

v5:reference:connection:getassoc

# 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
     ......
    )
    */

v5/reference/connection/getassoc.txt · Last modified: 2016/01/16 14:34 by mnewnham

### Page Tools

- Show pagesource
- Old revisions
- Backlinks
- Rename Page
- Back to top