Core\Db\Write::changed
$write->changed(): ?uintTells you the row count the server itself reported, or null where it reported none.
Core\Db\Connection::execute returns a Core\Db\Write, and changed is one of the counts on it. Its type is ?uint: a whole number, or null. Where it is a number, it is the same number Core\Db\Write::affected gives.
null means the statement has no row count at all. A create table is such a statement on PostgreSQL. affected turns that into 0, so changed is the only place where "the server gave no count" and "the statement changed no rows" still look different.
Not every server can tell those two apart. SQLite reports a count for every statement it runs, so on SQLite changed is never null.
Where your program needs a number to work with, test the count for null first.
The examples read the count a server reported, tell no count apart from no rows, and add up the rows a clean-up removed.
Return value
Returns ?uint. A ?uint. When it is not null, it is equal to affected. MySQL counts matched rows and changed rows separately. PostgreSQL does not send that difference, so there is no second count. A second count that always matched the first would mislead programs.
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 count the server reported
<?nvs// Shows how to read the row count a server reported for a write. The type is// `?uint`, so the count is a whole number or `null`.
var $db = Core\Db::connect('notes');$db->execute('create table notes (id integer primary key, text text not null, done boolean not null)', []);$db->execute( 'insert into notes (text, done) values (?, ?), (?, ?), (?, ?)', ['buy milk', false, 'call Ana', false, 'book train', true],);
var $write = $db->execute('update notes set done = ? where done = ?', [true, false]);
// `??` prints a `0` where the count is `null`. Two of the three notes were// still open, so this prints `2 rows changed`.echo $write->changed() ?? 0, " rows changed\n";
$db->close();2 rows changedTell no count apart from no rows
<?nvs// Shows the difference `changed` keeps and `affected` does not: a statement// that changed no rows, and a statement the server gave no count for.
var $db = Core\Db::connect('notes');$db->execute('create table notes (id integer primary key, text text not null)', []);$db->execute('insert into notes (text) values (?)', ['buy milk']);
var $write = $db->execute('update notes set text = ? where id = ?', ['buy bread', 99]);var $count = $write->changed();
if ($count == null) { // Some servers report no count for a statement such as `create table`. echo "the server reported no count\n";} else { // SQLite reports a count for every statement, so this is the line that // runs here. No note has id 99, so it prints `0 rows matched`. echo $count, " rows matched\n";}
// `affected` is the same count with the `null` folded to `0`. This prints `0`.echo $write->affected(), "\n";
$db->close();0 rows matched0Add up what a clean up removed
<?nvs// Shows the work a program really does here: a nightly clean-up deletes old// rows from two tables and reports how many rows it removed in total.
var $db = Core\Db::connect('notes');$db->execute('create table sessions (id integer primary key, seen int not null)', []);$db->execute('create table logs (id integer primary key, day int not null)', []);$db->execute('insert into sessions (seen) values (?), (?), (?)', [1, 2, 9]);$db->execute('insert into logs (day) values (?), (?)', [1, 9]);
class Cleanup{ // The count is a `?uint`, so it is tested for `null` before it is added. A // server that reports no count adds nothing, and the total still comes out. public static function remove(Core\Db\Connection $db, string $sql, int $before): uint { var $write = $db->execute($sql, [$before]); ?uint $count = $write->changed(); if ($count == null) { return 0; } return $count; }}
uint $sessions = Cleanup::remove($db, 'delete from sessions where seen < ?', 5);uint $logs = Cleanup::remove($db, 'delete from logs where day < ?', 5);
// Two old sessions and one old log line went. This prints `removed 3 rows`.echo 'removed ', $sessions + $logs, " rows\n";
$db->close();removed 3 rows