$sql = 'SELECT name FROM user WHERE id = 123';
$user_name = $GLOBALS['dbi']->fetchValue($sql);
produces
$user_name = 'John Doe'
public fetchValue ( string $query, integer $row_number, integer | string $field, object $link = null ) : mixed | ||
$query | string | The query to execute |
$row_number | integer | row to fetch the value from, starting at 0, with 0 being default |
$field | integer | string | field to fetch the value from, starting at 0, with 0 being default |
$link | object | mysql link |
Результат | mixed | value of first field in first row from result or false if not found |
public function fetchValue($query, $row_number = 0, $field = 0, $link = null)
{
$value = false;
$result = $this->tryQuery($query, $link, self::QUERY_STORE, false);
if ($result === false) {
return false;
}
// return false if result is empty or false
// or requested row is larger than rows in result
if ($this->numRows($result) < $row_number + 1) {
return $value;
}
// if $field is an integer use non associative mysql fetch function
if (is_int($field)) {
$fetch_function = 'fetchRow';
} else {
$fetch_function = 'fetchAssoc';
}
// get requested row
for ($i = 0; $i <= $row_number; $i++) {
$row = $this->{$fetch_function}($result);
}
$this->freeResult($result);
// return requested field
if (isset($row[$field])) {
$value = $row[$field];
}
return $value;
}
/** * Returns the CREATE statement for this table * * @return mixed */ public function showCreate() { return $this->_dbi->fetchValue('SHOW CREATE TABLE ' . Util::backquote($this->_db_name) . '.' . Util::backquote($this->_name), 0, 1); }