You are here

function DB_common::getAssoc in Flickr API 5

Fetches an entire query result and returns it as an associative array using the first column as the key

If the result set contains more than two columns, the value will be an array of the values from column 2-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). A DB error code is returned on errors. If the result set contains fewer than two columns, a DB_ERROR_TRUNCATED error is returned.

For example, if the table "mytable" contains:

<pre> ID TEXT DATE -------------------------------- 1 'one' 944679408 2 'two' 944679408 3 'three' 944679408 </pre>

Then the call getAssoc('SELECT id,text FROM mytable') returns: <pre> array( '1' => 'one', '2' => 'two', '3' => 'three', ) </pre>

...while the call getAssoc('SELECT id,text,date FROM mytable') returns: <pre> array( '1' => array('one', '944679408'), '2' => array('two', '944679408'), '3' => array('three', '944679408') ) </pre>

If the more than one row occurs with the same value in the first column, the last row overwrites all previous ones by default. Use the $group parameter if you don't want to overwrite like this. Example:

<pre> getAssoc('SELECT category,id,name FROM mytable', false, null, DB_FETCHMODE_ASSOC, true) returns:

array( '1' => array(array('id' => '4', 'name' => 'number four'), array('id' => '6', 'name' => 'number six') ), '9' => array(array('id' => '4', 'name' => 'number four'), array('id' => '6', 'name' => 'number six') ) ) </pre>

Keep in mind that database functions in PHP usually return string values for results regardless of the database's internal type.

Parameters

string $query the SQL query:

bool $force_array used only when 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 used in: execution of the statement. Quantity of items passed must match quantity of placeholders in query: meaning 1 placeholder for non-array parameters or 1 placeholder per array element.

int $fetchmode the fetch mode to use:

bool $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 the associative array containing the query results. A DB_Error object on failure.

File

phpFlickr/PEAR/DB/common.php, line 1467

Class

DB_common
DB_common is the base class from which each database driver class extends

Code

function &getAssoc($query, $force_array = false, $params = [], $fetchmode = DB_FETCHMODE_DEFAULT, $group = false) {
  $params = (array) $params;
  if (sizeof($params) > 0) {
    $sth = $this
      ->prepare($query);
    if (DB::isError($sth)) {
      return $sth;
    }
    $res =& $this
      ->execute($sth, $params);
    $this
      ->freePrepared($sth);
  }
  else {
    $res =& $this
      ->query($query);
  }
  if (DB::isError($res)) {
    return $res;
  }
  if ($fetchmode == DB_FETCHMODE_DEFAULT) {
    $fetchmode = $this->fetchmode;
  }
  $cols = $res
    ->numCols();
  if ($cols < 2) {
    $tmp =& $this
      ->raiseError(DB_ERROR_TRUNCATED);
    return $tmp;
  }
  $results = array();
  if ($cols > 2 || $force_array) {

    // return array values
    // XXX this part can be optimized
    if ($fetchmode == DB_FETCHMODE_ASSOC) {
      while (is_array($row = $res
        ->fetchRow(DB_FETCHMODE_ASSOC))) {
        reset($row);
        $key = current($row);
        unset($row[key($row)]);
        if ($group) {
          $results[$key][] = $row;
        }
        else {
          $results[$key] = $row;
        }
      }
    }
    elseif ($fetchmode == DB_FETCHMODE_OBJECT) {
      while ($row = $res
        ->fetchRow(DB_FETCHMODE_OBJECT)) {
        $arr = get_object_vars($row);
        $key = current($arr);
        if ($group) {
          $results[$key][] = $row;
        }
        else {
          $results[$key] = $row;
        }
      }
    }
    else {
      while (is_array($row = $res
        ->fetchRow(DB_FETCHMODE_ORDERED))) {

        // we shift away the first element to get
        // indices running from 0 again
        $key = array_shift($row);
        if ($group) {
          $results[$key][] = $row;
        }
        else {
          $results[$key] = $row;
        }
      }
    }
    if (DB::isError($row)) {
      $results = $row;
    }
  }
  else {

    // return scalar values
    while (is_array($row = $res
      ->fetchRow(DB_FETCHMODE_ORDERED))) {
      if ($group) {
        $results[$row[0]][] = $row[1];
      }
      else {
        $results[$row[0]] = $row[1];
      }
    }
    if (DB::isError($row)) {
      $results = $row;
    }
  }
  $res
    ->free();
  return $results;
}