Core\Db\Row::int
Reads one column of a row as a whole number.
Core\Db\Row::int takes the name of a column and returns the number in it. It reads the integer column types: SMALLINT, INT and BIGINT. An unsigned column is read too, as long as its value fits in an int.
The result is null when the column has no value in this row, so the type you get back is ?int. Test it for null, or write ?? 0 after the call to use zero instead.
The method throws a LogicError when the row has no column with that name, and when the column is not an integer column. A text column holding the characters 7 is still text, and a column with a fraction is read with Core\Db\Row::float or Core\Db\Row::decimal. It also throws for an unsigned value above 9223372036854775807, which is the largest number an int holds. Read that column with Core\Db\Row::uint.
The examples below read the number of seats at a table, read a column that may have no value, and compare two numbers out of the same row.
Parameters
$namestringThe column label, as the server described it.
Return value
Returns ?int. The integer, or null for a NULL column.
Errors
LogicErrorThe row has no column with that name, the column is not an integer, or it is an unsigned value larger than the largestint.
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 a whole number out of a row
<?nvs// Shows how to read a whole number out of a row.
var $db = Core\Db::connect('notes');$db->execute('create table tables (name text not null, seats int not null)', []);$db->execute( 'insert into tables (name, seats) values (?, ?), (?, ?)', ['by the window', 4, 'in the corner', 2],);
// `int` reads the named column as a whole number. This prints one line for// each table.foreach ($db->query('select name, seats from tables order by name', []) as Core\Db\Row $row) { echo $row->string('name') ?? '', ' has ', $row->int('seats') ?? 0, " seats\n";}
$db->close();by the window has 4 seatsin the corner has 2 seatsA 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 tables (name text not null, seats int)', []);$db->execute( 'insert into tables (name, seats) values (?, ?), (?, ?)', ['by the window', 4, 'on the terrace', null],);
// The result is `null` when the column has no value in this row. The type you// get back is `?int`, so you test it for `null` before you use it.foreach ($db->query('select name, seats from tables order by name', []) as Core\Db\Row $row) { ?int $seats = $row->int('seats'); if ($seats == null) { echo $row->string('name') ?? '', ': nobody counted the seats', "\n"; } else { echo $row->string('name') ?? '', ': ', $seats, " seats\n"; }}
$db->close();by the window: 4 seatson the terrace: nobody counted the seatsCheck that an order fits in stock
<?nvs// Shows how to work with two numbers out of the same row. This prints what can// be sent today and what has to wait.
var $db = Core\Db::connect('notes');$db->execute('create table lines (item text not null, ordered int not null, in_stock int not null)', []);$db->execute( 'insert into lines (item, ordered, in_stock) values (?, ?, ?), (?, ?, ?), (?, ?, ?)', ['tea', 12, 40, 'cups', 30, 30, 'spoons', 8, 3],);
foreach ($db->query('select item, ordered, in_stock from lines order by item', []) as Core\Db\Row $row) { int $ordered = $row->int('ordered') ?? 0; int $stock = $row->int('in_stock') ?? 0; if ($ordered <= $stock) { echo $row->string('item') ?? '', ': all ', $ordered, " can be sent\n"; } else { // Both numbers are whole, so this subtraction is exact. echo $row->string('item') ?? '', ': ', $ordered - $stock, " have to wait\n"; }}
$db->close();cups: all 30 can be sentspoons: 5 have to waittea: all 12 can be sent