Skip to content

Core\Db\Row::get

$row->get(string $name): mixed$row->get(
    string $name,
): mixed

Reads one column of a row without saying what type it is.

Core\Db\Row::get takes the name of a column and returns its value. The type of that value is the column's own: an INT column gives an int, and a TEXT column gives a string. Your program learns which one only while it runs, so the type of the result is mixed.

Write as after the call to turn the result into the type you need. $row->get('seats') as ?int gives you a whole number or null.

The result is null when the column has no value in this row. The method throws a LogicError when the row has no column with that name.

Where you know the type, a typed reader such as Core\Db\Row::int is the better choice, because it writes the type down and checks it. Use get in a program that works on tables it was not written for, such as a tool that copies rows.

The examples below read a column without naming its type, read a column that has no value, and copy rows into another table.

Parameters

  • $namestringThe column label, as the server described it.

Return value

Returns mixed. The value, or null for a NULL column.

Errors

  • LogicErrorThe row has no column with that name.

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 a column without naming its type

<?nvs
// Shows how to read a column without saying what type it is.
var $db = Core\Db::connect('notes');
$db->execute('create table tables (name text not null, seats int not null)', []);
$db->execute('insert into tables (name, seats) values (?, ?)', ['by the window', 4]);
array<Core\Db\Row> $rows = $db->query('select name, seats from tables', [])->all();
var $row = $rows[0];
// `get` returns the value the column holds, and its type is the column's own.
// `as` turns that value into the type this line needs.
?int $seats = $row->get('seats') as ?int;
?tainted string $name = $row->get('name') as ?string;
echo $name ?? '', ' has ', $seats ?? 0, " seats\n";
$db->close();
Output
by the window has 4 seats

A column that has no value

<?nvs
// Shows what you get when the column has no value in this row.
var $db = Core\Db::connect('notes');
$db->execute('create table tables (name text not null, seats int)', []);
$db->execute(
'insert into tables (name, seats) values (?, ?), (?, ?)',
['by the window', 4, 'on the terrace', null],
);
foreach ($db->query('select name, seats from tables order by name', []) as Core\Db\Row $row) {
// The result is `null` when the column has no value in this row. Test it
// for `null` before you turn it into a number.
var $seats = $row->get('seats');
if ($seats == null) {
echo $row->string('name') ?? '', ': nobody counted the seats', "\n";
} else {
echo $row->string('name') ?? '', ': ', $seats as ?int ?? 0, " seats\n";
}
}
$db->close();
Output
by the window: 4 seats
on the terrace: nobody counted the seats

Copy rows into another table

<?nvs
// Shows how to copy rows from one table into another. This program is not
// written for one table, so it reads every column with `get`.
var $db = Core\Db::connect('notes');
$db->execute(
'create table orders (item text not null, quantity int not null, price real not null)',
[],
);
$db->execute(
'create table archive (item text not null, quantity int not null, price real not null)',
[],
);
$db->execute(
'insert into orders (item, quantity, price) values (?, ?, ?), (?, ?, ?)',
['cups', 30, 2.25, 'tea', 12, 4.5],
);
array<string> $columns = ['item', 'quantity', 'price'];
foreach ($db->query('select item, quantity, price from orders order by item', []) as Core\Db\Row $row) {
// Each value keeps the type its column has, and the insert takes it as it
// is. Nothing here has to know what that type was.
array<mixed> $values = [];
foreach ($columns as string $name) {
$values[] = $row->get($name);
}
$db->execute('insert into archive (item, quantity, price) values (?, ?, ?)', $values);
}
foreach ($db->query('select item, quantity from archive order by item', []) as Core\Db\Row $copied) {
echo $copied->string('item') ?? '', ': ', $copied->int('quantity') ?? 0, "\n";
}
$db->close();
Output
cups: 30
tea: 12