getOne()
The getOne()
method executes a complete SELECT
query and returns exactly one row as a simple associative array. It is intended for detail views or queries where only one row is expected. If no result is found, it returns false
.
Signature:
public function getOne(string $table, string|array $columns = '*'): array|false
Basic Usage
$user = $db->getOne('users');
Select Specific Columns
$user = $db->getOne('users', ['id', 'email']);
Returned Format
getOne()
always returns a single associative array:
[
'id' => 7,
'email' => 'j.schulz@gmail.urx',
'created_at' => '2024-10-01 11:23:00',
...
]
Important:
setReturnKey()
must not be used with getOne()
.
Since getOne()
returns only one flat result, keying the result by column would make no sense.
If setReturnKey()
was used before, an exception will be thrown.
Handling No Results
If the query finds no match, false
is returned:
$product = $db->getOne('products', ['name', 'price']);
if ($product === false) {
echo "No result found.";
}
Security Notes
- All validations for table and column names are applied as in
get()
. - Always use secure WHERE clauses like
whereInt()
orwhereString()
beforehand.