getValue()
The getValue()
method retrieves a single column value from the first row of the result.
It is ideal when you only need one value — for example, an email, a price, or a count.
Signature:
public function getValue(string $table, string $column): mixed
Basic Usage
$email = $db->getValue('users', 'email');
This is equivalent to calling getOne()
and extracting a single column manually — but more efficient and direct.
With Conditions
As with any SELECT, you can define WHERE clauses before calling getValue()
:
$db->where('id', 100);
$username = $db->getValue('users', 'username');
With JOIN or GROUP BY
All clauses like join()
, groupBy()
, orderBy()
, and limit()
are supported:
$db->join('user_profiles p', 'u.id = p.user_id');
$db->where('u.status', 'active');
$age = $db->getValue('users u', 'p.age');
Return Value
The returned value is the raw scalar result from the selected column.
If no rows match, the return value is null
.
Security Notes
- Column and table names are validated before query execution.
getValue()
usesLIMIT 1
internally to reduce overhead.- All standard WHERE methods like
whereInt()
orwhereFunc()
are supported.