Skip to content

Core\Db\Rows::first

$rows->first(): ?T

Gives the first row a query answered, or null when it answered none.

Core\Db\Rows::first returns the first row of the result, in the order the query asked for. A query that matched nothing returns null, so a lookup that found nothing is an answer your program tests for, and not an error to catch.

There is no cursor to move on. Two calls return the first row both times, which is what makes it safe to read inside a condition and read again after it.

A result from queryAs returns an object of the class you named there, in the same way the whole result does.

The examples below read the one row a lookup found, say so when a lookup found nothing, and read the first row as an object of your own class.

Return value

Returns ?T. A T, as all describes it, or null for an empty result. Everywhere in Core, ?T means that a value can be missing. A query that matched nothing is not an error.

Examples

The examples below run with this configuration file.

nvs.toml
# The `notes` database is kept in memory. It is deleted when the program ends.
[db.notes]
driver = "sqlite"
path = ":memory:"
# The program may open the `notes` database.
[capabilities.db]
connect = ["notes"]

Read the one row a lookup found

<?nvs
// Shows how to read the single row a lookup by id found. `first` returns the
// first row of the result.
var $db = Core\Db::connect('notes');
$db->execute('create table guests (id integer primary key, name text not null, seats int not null)', []);
$db->execute('insert into guests (name, seats) values (?, ?), (?, ?)', ['Ada Ahmed', 4, 'Bo Berg', 2]);
var $rows = $db->query('select name, seats from guests where id = ?', [1]);
var $guest = $rows->first();
// This prints `Ada Ahmed has 4 seats`.
if ($guest != null) {
echo $guest->string('name') ?? '', ' has ', $guest->int('seats') ?? 0, " seats\n";
}
// There is no cursor to move on, so the same call gives the same row again.
// This prints `Ada Ahmed` a second time.
var $same = $rows->first();
if ($same != null) {
echo $same->string('name') ?? '', "\n";
}
$db->close();
Output
Ada Ahmed has 4 seats
Ada Ahmed

Say so when a lookup found nothing

<?nvs
// Shows how to handle a lookup that found nothing. `first` returns `null` when
// the query matched no rows, so your program can say so.
var $db = Core\Db::connect('notes');
$db->execute('create table guests (id integer primary key, name text not null)', []);
$db->execute('insert into guests (name) values (?)', ['Ada Ahmed']);
// No guest has id 99, so this prints `no such guest`.
var $missing = $db->query('select name from guests where id = ?', [99])->first();
if ($missing == null) {
echo "no such guest\n";
} else {
echo $missing->string('name') ?? '', "\n";
}
// Guest 1 is there, so this prints `Ada Ahmed`.
var $found = $db->query('select name from guests where id = ?', [1])->first();
if ($found == null) {
echo "no such guest\n";
} else {
echo $found->string('name') ?? '', "\n";
}
$db->close();
Output
no such guest
Ada Ahmed

Read the first row as your own class

<?nvs
// Shows how to read one row as an object of your own class. A program loads a
// single record this way, on a page that shows one order.
use Core\Db\Derive;
// `#[Derive]` writes the code that fills this class from a row. Each property
// takes the column of the same name.
#[Derive]
class Order
{
public uint $id;
// A text column arrives as `tainted`, because the value comes from the
// database.
public tainted string $customer;
public uint $cents;
public function constructor(uint $id, tainted string $customer, uint $cents)
{
$this->id = $id;
$this->customer = $customer;
$this->cents = $cents;
}
}
var $db = Core\Db::connect('notes');
$db->execute('create table orders (id integer primary key, customer text not null, cents int not null)', []);
$db->execute('insert into orders (customer, cents) values (?, ?), (?, ?)', ['Ada Ahmed', 1250, 'Bo Berg', 800]);
// `queryAs<Order>` builds `Order` objects, so `first` gives back an `Order`.
var $order = $db->queryAs<Order>('select id, customer, cents from orders where id = ?', [2])->first();
// This prints `Bo Berg pays 800`.
if ($order != null) {
echo $order->customer, ' pays ', $order->cents, "\n";
}
$db->close();
Output
Bo Berg pays 800