PMA\libraries\DatabaseInterface::fetchResult PHP Méthode

fetchResult() public méthode

$sql = 'SELECT * FROM user'; $users = $GLOBALS['dbi']->fetchResult($sql); produces $users[] = array('id' => 123, 'name' => 'John Doe') $sql = 'SELECT id, name FROM user'; $users = $GLOBALS['dbi']->fetchResult($sql, 'id'); produces $users['123'] = array('id' => 123, 'name' => 'John Doe') $sql = 'SELECT id, name FROM user'; $users = $GLOBALS['dbi']->fetchResult($sql, 0); produces $users['123'] = array(0 => 123, 1 => 'John Doe') $sql = 'SELECT id, name FROM user'; $users = $GLOBALS['dbi']->fetchResult($sql, 'id', 'name'); or $users = $GLOBALS['dbi']->fetchResult($sql, 0, 1); produces $users['123'] = 'John Doe' $sql = 'SELECT name FROM user'; $users = $GLOBALS['dbi']->fetchResult($sql); produces $users[] = 'John Doe' $sql = 'SELECT group, name FROM user' $users = $GLOBALS['dbi']->fetchResult($sql, array('group', null), 'name'); produces $users['admin'][] = 'John Doe' $sql = 'SELECT group, name FROM user' $users = $GLOBALS['dbi']->fetchResult($sql, array('group', 'name'), 'id'); produces $users['admin']['John Doe'] = '123'
public fetchResult ( string $query, string | integer | array $key = null, string | integer $value = null, object $link = null, integer $options ) : array
$query string query to execute
$key string | integer | array field-name or offset used as key for array or array of those
$value string | integer value-name or offset used as value for array
$link object mysql link
$options integer query options
Résultat array resultrows or values indexed by $key
    public function fetchResult($query, $key = null, $value = null, $link = null, $options = 0)
    {
        $resultrows = array();
        $result = $this->tryQuery($query, $link, $options, false);
        // return empty array if result is empty or false
        if ($result === false) {
            return $resultrows;
        }
        $fetch_function = 'fetchAssoc';
        // no nested array if only one field is in result
        if (null === $key && 1 === $this->numFields($result)) {
            $value = 0;
            $fetch_function = 'fetchRow';
        }
        // if $key is an integer use non associative mysql fetch function
        if (is_int($key)) {
            $fetch_function = 'fetchRow';
        }
        if (null === $key) {
            while ($row = $this->{$fetch_function}($result)) {
                $resultrows[] = $this->_fetchValue($row, $value);
            }
        } else {
            if (is_array($key)) {
                while ($row = $this->{$fetch_function}($result)) {
                    $result_target =& $resultrows;
                    foreach ($key as $key_index) {
                        if (null === $key_index) {
                            $result_target =& $result_target[];
                            continue;
                        }
                        if (!isset($result_target[$row[$key_index]])) {
                            $result_target[$row[$key_index]] = array();
                        }
                        $result_target =& $result_target[$row[$key_index]];
                    }
                    $result_target = $this->_fetchValue($row, $value);
                }
            } else {
                while ($row = $this->{$fetch_function}($result)) {
                    $resultrows[$row[$key]] = $this->_fetchValue($row, $value);
                }
            }
        }
        $this->freeResult($result);
        return $resultrows;
    }

Usage Example

Exemple #1
0
 /**
  * Returns the generation expression for virtual columns
  *
  * @param string $column name of the column
  *
  * @return array|boolean associative array of column name and their expressions
  *                       or false on failure
  */
 public function getColumnGenerationExpression($column = null)
 {
     $serverType = Util::getServerType();
     if ($serverType == 'MySQL' && PMA_MYSQL_INT_VERSION > 50705 && !$GLOBALS['cfg']['Server']['DisableIS']) {
         $sql = "SELECT\n                `COLUMN_NAME` AS `Field`,\n                `GENERATION_EXPRESSION` AS `Expression`\n                FROM\n                `information_schema`.`COLUMNS`\n                WHERE\n                `TABLE_SCHEMA` = '" . Util::sqlAddSlashes($this->_db_name) . "'\n                AND `TABLE_NAME` = '" . Util::sqlAddSlashes($this->_name) . "'";
         if ($column != null) {
             $sql .= " AND  `COLUMN_NAME` = '" . Util::sqlAddSlashes($column) . "'";
         }
         $columns = $this->_dbi->fetchResult($sql, 'Field', 'Expression');
         return $columns;
     }
     $createTable = $this->showCreate();
     if (!$createTable) {
         return false;
     }
     $parser = new Parser($createTable);
     /**
      * @var \SqlParser\Statements\CreateStatement $stmt
      */
     $stmt = $parser->statements[0];
     $fields = Table::getFields($stmt);
     if ($column != null) {
         $expression = isset($fields[$column]['expr']) ? substr($fields[$column]['expr'], 1, -1) : '';
         return array($column => $expression);
     }
     $ret = array();
     foreach ($fields as $field => $options) {
         if (isset($options['expr'])) {
             $ret[$field] = substr($options['expr'], 1, -1);
         }
     }
     return $ret;
 }