$sql = 'SELECT * FROM user WHERE id = 123';
$user = $GLOBALS['dbi']->fetchSingleRow($sql);
produces
$user = array('id' => 123, 'name' => 'John Doe')
public fetchSingleRow ( string $query, string $type = 'ASSOC', object $link = null ) : array | boolean | ||
$query | string | The query to execute |
$type | string | NUM|ASSOC|BOTH returned array should either numeric associative or both |
$link | object | mysql link |
return | array | boolean | first row from result or false if result is empty |
public function fetchSingleRow($query, $type = 'ASSOC', $link = null)
{
$result = $this->tryQuery($query, $link, self::QUERY_STORE, false);
if ($result === false) {
return false;
}
// return false if result is empty or false
if (!$this->numRows($result)) {
return false;
}
switch ($type) {
case 'NUM':
$fetch_function = 'fetchRow';
break;
case 'ASSOC':
$fetch_function = 'fetchAssoc';
break;
case 'BOTH':
default:
$fetch_function = 'fetchArray';
break;
}
$row = $this->{$fetch_function}($result);
$this->freeResult($result);
return $row;
}
/** * Returns the real row count for a table * * @return number */ function getRealRowCountTable() { // SQL query to get row count for a table. $result = $this->_dbi->fetchSingleRow(sprintf('SELECT COUNT(*) AS %s FROM %s.%s', Util::backquote('row_count'), Util::backquote($this->_db_name), Util::backquote($this->_name))); return $result['row_count']; }