Core\Db\Rows::value
$rows->value(): mixedGives the first column of the first row a query answered.
Core\Db\Rows::value reads a single value out of a query that answers one. A select count(*) is the everyday case: the result has one row with one column, and this method gives you that one value.
The type of the result is the column's own, so the method returns mixed. Write as ?int or as ?string after the call to turn it into the type your program needs.
The result is null when the query matched no rows, and null as well when the first column of the first row has no value. The two are not told apart. Core\Db\Rows::count is what separates them: a result with no rows counts 0, and a result with one row counts 1.
The examples below read the number a count query answered, show the two times the result is null, and build a summary line out of three one-value queries.
Return value
Returns mixed. That column's value, or null if the result has no rows. A NULL column also gives null, because the declared type is mixed. To tell the two apart, call count() first.
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 the number a count query answered
<?nvs// Shows how to read the number a `select count(*)` answered. `value` gives the// first column of the first row.
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],);
// The result has one row with one column, so `value` is the whole answer. `as// ?int` turns it into a whole number. This prints `3 guests`.var $rows = $db->query('select count(*) from guests', []);echo $rows->value() as ?int ?? 0, " guests\n";
// Any query that answers one value is read the same way. This prints// `12 seats`.var $seats = $db->query('select sum(seats) from guests', [])->value();echo $seats as ?int ?? 0, " seats\n";
$db->close();3 guests12 seatsThe two times the value is null
<?nvs// Shows the two times `value` gives `null`: the query matched no rows, and the// first column of the first row has no value. `Core\Db\Rows::count` is what// tells the two apart.
var $db = Core\Db::connect('notes');$db->execute('create table guests (id integer primary key, name text not null, seats int)', []);$db->execute('insert into guests (name, seats) values (?, ?)', ['Ada Ahmed', null]);
// No guest has id 99, so the query matched no rows at all. This prints// `no value, rows: 0`.var $missing = $db->query('select seats from guests where id = ?', [99]);if ($missing->value() == null) { echo 'no value, rows: ', $missing->count(), "\n";}
// Guest 1 is there, and its `seats` column has no value. `value` gives `null`// here as well. This prints `no value, rows: 1`.var $present = $db->query('select seats from guests where id = ?', [1]);if ($present->value() == null) { echo 'no value, rows: ', $present->count(), "\n";}
// The count is what your program asks when it has to know which of the two it// is looking at.$db->close();no value, rows: 0no value, rows: 1Build a summary line from one value queries
<?nvs// Shows how a summary line is built from queries that each answer one value. A// report page works this way for every figure it prints.
var $db = Core\Db::connect('notes');$db->execute('create table orders (id integer primary key, customer text not null, cents int not null)', []);$db->execute( 'insert into orders (customer, cents) values (?, ?), (?, ?), (?, ?)', ['Ada Ahmed', 1250, 'Bo Berg', 800, 'Cem Celik', 2400],);
// Each query answers one row with one column, so each figure is one `value`// call. The default after `??` covers a table with no orders in it yet.var $orders = $db->query('select count(*) from orders', [])->value() as ?int ?? 0;var $cents = $db->query('select sum(cents) from orders', [])->value() as ?int ?? 0;var $best = $db->query('select customer from orders order by cents desc limit 1', [])->value() as ?string ?? 'nobody';
// This prints `3 orders, 4450 cents, best customer: Cem Celik`.echo $orders, ' orders, ', $cents, ' cents, best customer: ', $best, "\n";
$db->close();3 orders, 4450 cents, best customer: Cem Celik