Core\Db\Rows::count
$rows->count(): uintSays how many rows a query returned.
Core\Db\Rows::count returns the number of rows the statement returned, as a whole number. The number is exact. Novis reads every row before query returns, so this is the length of a result you already have. It is never an estimate.
A query that matched nothing gives 0. Counting does not use the rows up. You can count a result, read it, and count it again, and the number is the same every time.
This counts the rows a select returned. For the number of rows an insert, an update or a delete changed, read Core\Db\Write::affected instead.
The examples below show how many rows a search found, and do something else when nothing matched. The last one works out how many pages a list of results needs.
Return value
Returns uint. The number of rows in the result. It is exact, because query reads all rows before it returns.
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"]Say how many rows a search found
<?nvs// Shows how to say how many rows a search found. `count` returns the number of// rows the query answered.
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 $found = $db->query('select name from guests where seats > ? order by name', [1]);
// This prints `3 guests found`.echo $found->count(), " guests found\n";
// Counting does not use the rows up. This prints the three names, and then `3`// a second time.foreach ($found->all() as Core\Db\Row $guest) { echo $guest->string('name') ?? '', "\n";}echo $found->count(), "\n";
$db->close();3 guests foundAda AhmedBo BergCem Celik3Take another path when nothing matched
<?nvs// Shows how to tell an empty result from a full one. A query that matched// nothing answers 0, so your program can say so instead of printing a blank// list.
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]);
var $mine = $db->query('select cents from orders where customer = ?', ['Bo Berg']);
// Bo Berg has no orders, so this prints `no orders yet`.if ($mine->count() == 0) { echo "no orders yet\n";} else { echo $mine->count(), " orders\n";}
// Ada Ahmed has one, so this prints `1 orders`.var $hers = $db->query('select cents from orders where customer = ?', ['Ada Ahmed']);if ($hers->count() == 0) { echo "no orders yet\n";} else { echo $hers->count(), " orders\n";}
$db->close();no orders yet1 ordersWork out how many pages a list needs
<?nvs// Shows how to work out the number of pages a list of results needs. The// calculation needs only the number of rows and the size of a page.
var $db = Core\Db::connect('notes');$db->execute('create table products (id integer primary key, title text not null)', []);$db->execute( 'insert into products (title) with recursive counter(x) as (select 1 union all select x + 1 from counter where x < 47) select \'item\' from counter', [],);
var $rows = $db->query('select id, title from products order by id', []);
// `count` returns a `uint`. `Core\Math::quotient` takes `int` values, so the// count is converted with `as int`.int $perPage = 10;int $found = $rows->count() as int;
// A last page that is not full still needs a page of its own. Adding nine// before the division counts it. This prints `47 products on 5 pages`.int $pages = Core\Math::quotient($found + $perPage - 1, $perPage);echo $found, ' products on ', $pages, " pages\n";
$db->close();47 products on 5 pages