Core\Db\Rows::column
Reads one column of a query as a plain list.
Core\Db\Rows::column returns the value that one column holds in every row. There is one entry per row, and the entries come in the same order as the rows.
Name the column with a string, or give its position as a whole number. The first column of the query is at position 0. A position is the way to read a column with no name of its own, such as the count(*) of a counting query.
A name that is no column of the result throws a LogicError. So does a position below zero or past the last column. A result with no rows returns an empty array for any name, because it describes no column for the name to be wrong about.
Each entry is the value the row already holds, so a column of large values costs no copy.
The examples below read one column from every row, name a column by its position, and answer a request with one column as JSON.
Parameters
$keyint|stringThe column. Anintis its position in the server's column list, counted from zero. Astringis its label.
Return value
Returns array<mixed>. An array<mixed> with one entry per row, empty for a result with no rows.
Errors
LogicErrorNo column has that name, or the position is negative or past the last column. An empty result returns an empty array and does not throw.
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"]Read one column from every row
<?nvs// Shows how to read one column of a query as a plain list.
var $db = Core\Db::connect('notes');$db->execute('create table guests (id integer primary key, name text not null, seats int not null)', []);$db->execute('insert into guests (name, seats) values (?, ?), (?, ?), (?, ?)', ['Ada Ahmed', 4, 'Bo Berg', 2, 'Cem Celik', 6]);
var $rows = $db->query('select name, seats from guests order by id', []);
// `column` returns the value this column holds in every row. The entries come// in the order the query asked for.array<mixed> $names = $rows->column('name');
// This prints the three names, one per line.foreach ($names as mixed $name) { echo $name as ?string ?? '', "\n";}
// You can ask the same result set for another column. This prints `12`.int $seats = 0;foreach ($rows->column('seats') as mixed $one) { $seats = $seats + ($one as ?int ?? 0);}echo $seats, "\n";
$db->close();Ada AhmedBo BergCem Celik12Name the column by its position
<?nvs// Shows how to read a column by its position instead of by its name.
var $db = Core\Db::connect('notes');$db->execute('create table readings (id integer primary key, taken_at text not null, degrees real not null)', []);$db->execute('insert into readings (taken_at, degrees) values (?, ?), (?, ?)', ['08:00', 11.5, '12:00', 18.25]);
// `count(*)` has no name of its own, so the position is the way to read it.// The first column of the query is at position 0.array<mixed> $total = $db->query('select count(*) from readings', [])->column(0);
// This prints `rows: 2`.echo 'rows: ', $total[0] as ?int ?? 0, "\n";
var $rows = $db->query('select taken_at, degrees from readings order by id', []);
// Position 1 is the second column of the query, which is `degrees` here.// This prints `11.5` and `18.25`, one per line.foreach ($rows->column(1) as mixed $degrees) { echo $degrees as ?float ?? 0.0, "\n";}
// A position that is no column of the result throws a `LogicError`. This// prints `no third column`.try { $rows->column(2);} catch (LogicError $missing) { echo "no third column\n";}
$db->close();rows: 211.518.25no third columnAnswer a request with one column as json
<?nvs// Shows how to send one column of a query as a JSON list. A program answers a// request for names or ids this way.
var $db = Core\Db::connect('notes');$db->execute('create table products (id integer primary key, sku text not null, title text not null)', []);$db->execute('insert into products (sku, title) values (?, ?), (?, ?), (?, ?)', ['A-1', 'Tea cup', 'A-2', 'Tea pot', 'B-7', 'Coffee mug']);
var $rows = $db->query('select sku from products order by sku', []);
// One call gives the array `Core\Json::encode` needs. A single column becomes// a JSON list, and one row per entry.array<mixed> $skus = $rows->column('sku');
// This prints `["A-1","A-2","B-7"]`.echo Core\Json::encode($skus), "\n";
$db->close();["A-1","A-2","B-7"]