Skip to content

Core\Db\Row::string

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

Reads one column of a row as text.

Core\Db\Row::string takes the name of a column and returns the text in it. It reads the text family of column types and nothing else: CHAR, VARCHAR, TEXT, ENUM and JSON.

The result is null when the column has no value in this row, so the type you get back is ?string. Test it for null, or write ?? '' after the call to use an empty text instead.

The method throws a LogicError when the row has no column with that name, and when the column is not one of those types. A number is not written out as text here. A BINARY, BLOB or BYTEA column is read with Core\Db\Row::bytes, and a UUID column with Core\Db\Row::uuid, even though an id looks like text when you print it.

The examples below read the name and the city of a person, read a column that may have no value, and count the orders of each status.

Parameters

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

Return value

Returns ?tainted string. The text, or null for a NULL column.

Errors

  • LogicErrorThe row has no column with that name, or the column is not text. A BYTEA is bytes, so read it with ->bytes. A number is not converted to text here.

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 name out of a row

<?nvs
// Shows how to read a text column out of a row.
var $db = Core\Db::connect('notes');
$db->execute('create table people (name text not null, city text not null)', []);
$db->execute(
'insert into people (name, city) values (?, ?), (?, ?)',
['Amina', 'Graz', 'Bruno', 'Lisboa'],
);
// `string` reads the named column as text. This prints one line for each person.
foreach ($db->query('select name, city from people order by name', []) as Core\Db\Row $row) {
echo $row->string('name') ?? '', ' lives in ', $row->string('city') ?? '', "\n";
}
$db->close();
Output
Amina lives in Graz
Bruno lives in Lisboa

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 people (name text not null, nickname text)', []);
$db->execute(
'insert into people (name, nickname) values (?, ?), (?, ?)',
['Amina', 'Mina', 'Bruno', null],
);
// `string` returns a `tainted string`, or `null` when the column has no value in
// this row. Test the result for `null` before you use it.
foreach ($db->query('select name, nickname from people order by name', []) as Core\Db\Row $row) {
?tainted string $nickname = $row->string('nickname');
if ($nickname == null) {
echo $row->string('name') ?? '', ' has no nickname', "\n";
} else {
echo $row->string('name') ?? '', ' is called ', $nickname, "\n";
}
}
$db->close();
Output
Amina is called Mina
Bruno has no nickname

Count the orders of each status

<?nvs
// Shows how to build a summary. The `status` column holds text, and that text
// is used as the key of an array of counts.
var $db = Core\Db::connect('notes');
$db->execute('create table orders (label text not null, status text not null)', []);
$db->execute(
'insert into orders (label, status) values (?, ?), (?, ?), (?, ?), (?, ?)',
['one tea', 'paid', 'two coffees', 'open', 'a sandwich', 'paid', 'a cake', 'sent'],
);
// Every key of this array is a status, and every value is a count.
array<string> $known = ['open', 'paid', 'sent'];
array<uint> $counted = [];
foreach ($known as string $status) {
$counted[$status] = 0;
}
foreach ($db->query('select status from orders', []) as Core\Db\Row $row) {
?tainted string $seen = $row->string('status');
// The text comes from the database, so it is `tainted`. The key is the
// known status that it is equal to.
foreach ($known as string $status) {
if ($seen == $status) {
$counted[$status] = $counted[$status] + 1;
}
}
}
// This prints `open 1`, `paid 2` and `sent 1`.
foreach ($known as string $status) {
echo $status, ' ', $counted[$status], "\n";
}
$db->close();
Output
open 1
paid 2
sent 1