Core\Db\Row::has
Tests whether the row has a column with this name.
Core\Db\Row::has takes the name of a column and returns true or false. It returns true for every column the row carries. A column with no value in this row is still a column, so the answer for it is true as well. It returns false for a name that belongs to no column of this row.
This method never throws an error, and that is what it is for. The typed readers do throw: both Core\Db\Row::int and Core\Db\Row::string throw a LogicError when the row has no column with that name. Test the name with has first, and you read it without catching an error. That matters when the name came in with a request, because then your program did not choose it.
The examples below test a column before reading it, read rows from two tables with one function, and read only the columns a request asked for.
Parameters
$namestringThe column label, as the server described it. It may betainted, because this method returns a result and does not throw.
Return value
Returns bool. true if the row has the column, whatever its value. A NULL column is present. false otherwise.
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"]Test a column before you read it
<?nvs// Shows how to test that a column is there before you read it.
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 (?, ?), (?, ?)', ['Ada', 'Addy', 'Bo', null],);
foreach ($db->query('select name, nickname from people order by name', []) as Core\Db\Row $row) { // `has` returns `true` for every column the row carries. A column with no // value in this row is still a column, so both rows print this line. if ($row->has('nickname')) { echo $row->string('name') ?? '', ' has a nickname column: ', $row->string('nickname') ?? '(no nickname)', "\n"; } // No column of this row has that name, so the answer is `false`. if (!$row->has('title')) { echo $row->string('name') ?? '', " has no title column\n"; }}
$db->close();Ada has a nickname column: AddyAda has no title columnBo has a nickname column: (no nickname)Bo has no title columnOne function for rows of two tables
<?nvs// Shows how one function reads rows that do not carry the same columns.
var $db = Core\Db::connect('notes');$db->execute('create table books (title text not null, pages int not null)', []);$db->execute('create table songs (title text not null, seconds int not null)', []);$db->execute('insert into books (title, pages) values (?, ?)', ['The Long Road', 412]);$db->execute('insert into songs (title, seconds) values (?, ?)', ['Morning Light', 218]);
class Library{ // The row comes from one table or the other, so the function tests each // column name before it reads that column. public static function describe(Core\Db\Row $row): void { echo $row->string('title') ?? ''; if ($row->has('pages')) { echo ', ', $row->int('pages') ?? 0, " pages\n"; return; } if ($row->has('seconds')) { echo ', ', $row->int('seconds') ?? 0, " seconds\n"; return; } echo "\n"; }}
foreach ($db->query('select title, pages from books', []) as Core\Db\Row $book) { Library::describe($book);}foreach ($db->query('select title, seconds from songs', []) as Core\Db\Row $song) { Library::describe($song);}
$db->close();The Long Road, 412 pagesMorning Light, 218 secondsRead only the columns a request asked for
<?nvs// Shows how to read the columns a request asked for. Your program did not// choose these names, so it tests each one before it reads that column.
var $db = Core\Db::connect('notes');$db->execute( 'create table stock (item text not null, on_hand int not null, reserved int not null)', [],);$db->execute('insert into stock (item, on_hand, reserved) values (?, ?, ?)', ['tea', 40, 12]);
// These three names came in with a request. One of them belongs to no column.array<string> $wanted = ['on_hand', 'price', 'reserved'];
array<Core\Db\Row> $rows = $db->query('select item, on_hand, reserved from stock', [])->all();var $row = $rows[0];foreach ($wanted as string $name) { if ($row->has($name)) { echo $name, ': ', $row->int($name) ?? 0, "\n"; } else { echo $name, ": there is no such column\n"; }}
$db->close();on_hand: 40price: there is no such columnreserved: 12