Core\Db\Rows::columns
$rows->columns(): array<Core\Db\Column>Describes the columns a query answered, one Core\Db\Column for each.
Core\Db\Rows::columns returns what the statement described. There is one entry per column, and the entries come in the order the query asked for them. Each entry is a Core\Db\Column, which gives you the column's name, the type it was declared as, and whether it may hold null.
The description belongs to the statement, not to the rows. A query that matched no rows describes its columns too, so a program can print the headings of a table before it knows whether there is anything under them. For the same reason queryAs describes the columns the query asked for, and the class it builds has nothing to say about them.
Every call returns the same description, and none of them costs a copy.
The examples below list the columns a query answered, print the headings when no rows matched, and write the first line of a CSV export.
Return value
Returns array<Core\Db\Column>. The columns. An empty result has them too. A select that matched nothing still describes the columns it would return, so you can read them before any row.
Examples
The examples below run with this configuration file.
# 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"]List the columns a query answered
<?nvs// Shows how to read the description of a query. `columns` returns one// `Core\Db\Column` for each column the query asked for.
var $db = Core\Db::connect('notes');$db->execute('create table notes (id integer primary key, title text not null, weight real)', []);$db->execute('insert into notes (title, weight) values (?, ?), (?, ?)', ['First note', 1.5, 'Second note', 2.5]);
var $rows = $db->query('select id, title, weight from notes order by id', []);
// The entries come in the order the query wrote them. This prints `id`, `title`// and `weight`, one per line.foreach ($rows->columns() as Core\Db\Column $column) { echo $column->name(), "\n";}
// One entry per column, so counting the entries counts the columns. This// prints `3`.echo Core\Arr::count($rows->columns()), "\n";
$db->close();idtitleweight3Print the headings when no rows matched
<?nvs// Shows that a query describes its columns even when it matched no rows. Your// program can print the headings of a table before it knows what is under them.
var $db = Core\Db::connect('notes');$db->execute('create table invoices (id integer primary key, number text not null, total real not null)', []);
// Nothing was inserted, so this query matches no rows. This prints `0`.var $rows = $db->query('select number, total from invoices', []);echo $rows->count(), "\n";
// The columns are described all the same. This prints `number` and `total`.foreach ($rows->columns() as Core\Db\Column $column) { echo $column->name(), "\n";}
$db->close();0numbertotalWrite the first line of a csv export
<?nvs// Shows how to write the heading line of a CSV export. The headings come from// the query, so a column added to the query is added to the file as well.
var $db = Core\Db::connect('notes');$db->execute('create table invoices (id integer primary key, number text not null, total real not null)', []);$db->execute('insert into invoices (number, total) values (?, ?), (?, ?)', ['2026-001', 19.5, '2026-002', 8.0]);
var $rows = $db->query('select number, total from invoices order by id', []);
array<string> $headings = [];foreach ($rows->columns() as Core\Db\Column $column) { $headings[] = $column->name();}
// This prints `number,total`.echo Core\Str::join($headings, ','), "\n";
$db->close();number,total