Skip to content

Core\Db\Column::name

$column->name(): string

Gives the label of one column in a query result.

A query result describes its columns before you read any rows. name() returns the label the database gave one column. When the query writes as, that alias is the label. When it does not, the label is the column name from the table.

You can print the label as a heading, and you can hand it to a row to read that column. The description belongs to the statement, not to a row, so the labels are there even when the query matched nothing.

Good to know: two columns can carry the same label. select id, id from notes describes two columns, and both are called id.

Return value

Returns string. The label. It is not a unique key: select a, a gives two columns with one label. So read a column by its position in the array that columns() returns.

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"]

Print the headings of a query

<?nvs
// Shows how to read the headings of a query. `columns()` describes what the
// statement returns, and `name()` gives the label of one column.
var $db = Core\Db::connect('notes');
$db->execute('create table notes (id integer primary key, text text not null)', []);
$db->execute('insert into notes (id, text) values (?, ?)', [1, 'buy milk']);
var $rows = $db->query('select id, text from notes order by id', []);
// This prints `id` and then `text`, one label per line.
foreach ($rows->columns() as Core\Db\Column $column) {
echo $column->name(), "\n";
}
$db->close();
Output
id
text

The alias becomes the label

<?nvs
// Shows what `as` in a query does to the label. The database describes the
// alias, so `name()` returns the alias and not the column in the table.
var $db = Core\Db::connect('notes');
$db->execute('create table notes (id integer primary key, text text not null)', []);
// The table calls these columns `id` and `text`.
var $rows = $db->query('select id as reference, text as body from notes', []);
// This prints `reference` and then `body`. The table is empty, and the labels
// are still there: a description belongs to the query.
foreach ($rows->columns() as Core\Db\Column $column) {
echo $column->name(), "\n";
}
$db->close();
Output
reference
body

Print a query you did not write

<?nvs
// Shows how to print the result of a query your code did not write, such as a
// report an operator saved. The labels are the heading line, and each label
// reads its own column out of every row.
var $db = Core\Db::connect('notes');
$db->execute('create table notes (id integer primary key, title text not null)', []);
$db->execute('insert into notes (title) values (?)', ['buy milk']);
$db->execute('insert into notes (title) values (?)', ['call the plumber']);
var $rows = $db->query('select id, title from notes order by id', []);
array<Core\Db\Column> $columns = $rows->columns();
string $heading = '';
foreach ($columns as Core\Db\Column $column) {
$heading = $heading . $column->name() . ';';
}
echo $heading, "\n";
// This prints `1;buy milk;` and `2;call the plumber;`.
foreach ($rows as Core\Db\Row $row) {
tainted string $line = '';
foreach ($columns as Core\Db\Column $cell) {
?tainted string $text = $row->get($cell->name()) as ?string;
$line = $line . ($text ?? '') . ';';
}
echo $line, "\n";
}
$db->close();
Output
id;title;
1;buy milk;
2;call the plumber;