Skip to content

Core\Db\Row::uint

$row->uint(string $name): ?uint$row->uint(
    string $name,
): ?uint

Reads one column of a row as a whole number that is never negative.

Core\Db\Row::uint takes the name of a column and returns the number in it. It reads the unsigned integer columns MySQL and MariaDB write as … UNSIGNED, and it reads a signed column too, as long as the value in this row is not negative.

The result is null when the column has no value in this row, so the type you get back is ?uint. Test it for null before you use it.

The method throws a LogicError when the row has no column with that name, when the column is not an integer column, and when the value is negative. A count, a size and a version number are never negative, which is what this reader is for. Where a column may hold a negative number, read it with Core\Db\Row::int.

The examples below read the size of a file, read a column that may have no value, and add up the sizes in a folder.

Parameters

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

Return value

Returns ?uint. The integer, or null for a NULL column.

Errors

  • LogicErrorThe row has no column with that name, the column is not an integer, or its value is negative. A negative value cannot be converted to uint.

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 count that is never negative

<?nvs
// Shows how to read a column that never holds a negative number.
var $db = Core\Db::connect('notes');
$db->execute('create table uploads (name text not null, size bigint not null)', []);
$db->execute(
'insert into uploads (name, size) values (?, ?), (?, ?)',
['logo.png', 18432, 'report.pdf', 1048576],
);
// `uint` reads the named column as a whole number that is never negative.
// This prints one line for each file.
foreach ($db->query('select name, size from uploads order by name', []) as Core\Db\Row $row) {
echo $row->string('name') ?? '', ': ', $row->uint('size') ?? 0, " bytes\n";
}
$db->close();
Output
logo.png: 18432 bytes
report.pdf: 1048576 bytes

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 uploads (name text not null, size bigint)', []);
$db->execute(
'insert into uploads (name, size) values (?, ?), (?, ?)',
['logo.png', 18432, 'draft.txt', null],
);
// The result is `null` when the column has no value in this row. The type you
// get back is `?uint`, so you test it for `null` before you use it.
foreach ($db->query('select name, size from uploads order by name', []) as Core\Db\Row $row) {
?uint $size = $row->uint('size');
if ($size == null) {
echo $row->string('name') ?? '', ': the size is not known', "\n";
} else {
echo $row->string('name') ?? '', ': ', $size, " bytes\n";
}
}
$db->close();
Output
draft.txt: the size is not known
logo.png: 18432 bytes

Add up what a folder holds

<?nvs
// Shows how to add up a column. Every size is a whole number that is never
// negative, so the total is one too.
var $db = Core\Db::connect('notes');
$db->execute('create table uploads (name text not null, folder text not null, size bigint not null)', []);
$db->execute(
'insert into uploads (name, folder, size) values (?, ?, ?), (?, ?, ?), (?, ?, ?)',
['logo.png', 'pictures', 18432, 'map.png', 'pictures', 40960, 'report.pdf', 'papers', 1048576],
);
uint $total = 0;
uint $files = 0;
foreach ($db->query('select size from uploads where folder = ?', ['pictures']) as Core\Db\Row $row) {
?uint $size = $row->uint('size');
if ($size != null) {
$total = $total + $size;
$files = $files + 1;
}
}
// This prints `2 files, 59392 bytes`.
echo $files, ' files, ', $total, " bytes\n";
$db->close();
Output
2 files, 59392 bytes