Skip to content

Core\Db\Row::bool

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

Reads one column of a row as a truth value.

Core\Db\Row::bool takes the name of a column and returns true or false. The column is a boolean column, or a whole number column that holds 0 or 1. MySQL and MariaDB have no boolean column of their own, so a TINYINT(1) column there is read with this method too.

The result is null when the column has no value in this row, so the type you get back is ?bool. Write ?? false to choose what your program uses instead.

The method throws a LogicError when the row has no column with that name, when the column holds text, a number with a fraction or a date, and when a whole number column holds any other number. A stored 7 is an error, not true.

The examples below read a yes or no column, read a column that may have no value, and read a flag that is stored as 0 or 1.

Parameters

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

Return value

Returns ?bool. The truth value, or null for a NULL column.

Errors

  • LogicErrorThe row has no column with that name, or the column is neither boolean nor an integer. It is also thrown for an integer other than 0 or 1. A stored 7 throws this 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 a yes or no column

<?nvs
// Shows how to read a column that holds yes or no.
var $db = Core\Db::connect('notes');
$db->execute('create table people (id integer primary key, name text not null, active boolean not null)', []);
$db->execute('insert into people (name, active) values (?, ?), (?, ?)', ['Ana', true, 'Bo', false]);
// `bool` reads the named column of the row as a truth value. This prints
// `Ana: active` and `Bo: not active`.
foreach ($db->query('select name, active from people order by name', []) as Core\Db\Row $row) {
bool $active = $row->bool('active') ?? false;
echo $row->string('name') ?? '', ': ', $active ? 'active' : 'not active', "\n";
}
$db->close();
Output
Ana: active
Bo: not active

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 settings (name text not null, enabled boolean)', []);
$db->execute('insert into settings (name, enabled) values (?, ?), (?, ?)', ['beta', null, 'dark mode', true]);
// `bool` returns `null` for a column with no value. `?? false` says which
// value your program uses instead. This prints `beta: off` and `dark mode: on`.
foreach ($db->query('select name, enabled from settings order by name', []) as Core\Db\Row $row) {
bool $enabled = $row->bool('enabled') ?? false;
echo $row->string('name') ?? '', ': ', $enabled ? 'on' : 'off', "\n";
}
$db->close();
Output
beta: off
dark mode: on

Pick the accounts a flag is set on

<?nvs
// Shows how to build the list of accounts a newsletter goes to. The two flags
// are whole number columns holding 0 or 1, which is how MySQL stores them.
var $db = Core\Db::connect('notes');
$db->execute('create table accounts (email text not null, newsletter int not null, verified int not null)', []);
$db->execute(
'insert into accounts (email, newsletter, verified) values (?, ?, ?), (?, ?, ?), (?, ?, ?)',
['ana@example.com', 1, 1, 'bo@example.com', 1, 0, 'cy@example.com', 0, 1],
);
// `bool` reads a whole number column holding 0 or 1 as a truth value. An
// account is added only when both flags are set.
array<tainted string> $send = [];
foreach ($db->query('select email, newsletter, verified from accounts order by email', []) as Core\Db\Row $row) {
if (($row->bool('newsletter') ?? false) && ($row->bool('verified') ?? false)) {
$send[] = $row->string('email') ?? '';
}
}
// This prints one address, `ana@example.com`.
foreach ($send as tainted string $address) {
echo $address, "\n";
}
$db->close();
Output
ana@example.com