Core\Db\Write::affected
$write->affected(): uintTells you how many rows a statement changed.
Core\Db\Connection::execute returns a Core\Db\Write, and affected is the count on it. It is a whole number and never null, so it needs no unwrapping: an insert of three rows answers 3, and an update that matched two rows answers 2.
An update or a delete whose where matched no row answers 0. This is the answer most programs act on. It says that the row you meant to change is not there, or that somebody else changed it first, which is what makes a conditional update a way to let exactly one worker take a job.
A statement that has no rows to count, such as create table, also answers 0. Core\Db\Write::changed is where those two are told apart.
The count belongs to the statement that answered it. Later statements on the same connection do not change what an earlier Core\Db\Write says.
The examples count the rows an update changed, find out that nothing matched, and let one worker take a job.
Return value
Returns uint. A uint. It is 0 for a statement that changed no rows. It is also 0 for a statement type that reports no count, such as create table. Use changed to tell the two apart.
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"]Count the rows an update changed
<?nvs// Shows how to read how many rows a write changed. `Core\Db\Connection::execute`// returns a `Core\Db\Write`, and `affected` is the count on it.
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]);
// Two of the three notes were still open, so two rows changed. This prints// `2 rows changed`.echo $write->affected(), " rows changed\n";
$db->close();2 rows changedFind out that nothing matched
<?nvs// Shows what the count is when a write matched no row. It is 0, so a program// can tell that the row it meant to change is not there.
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]);
if ($write->affected() == 0) { // This prints `there is no note 99`. echo "there is no note 99\n";}
// The note that is there was left alone. This prints `buy milk`.foreach ($db->query('select text from notes order by id', []) as Core\Db\Row $row) { echo $row->string('text') ?? '', "\n";}
$db->close();there is no note 99buy milkLet one worker take the job
<?nvs// Shows the work a program really does here: two workers try to take the same// job, and the count tells each of them whether it got it.
var $db = Core\Db::connect('notes');$db->execute('create table jobs (id integer primary key, taken boolean not null)', []);$db->execute('insert into jobs (taken) values (?)', [false]);
class Worker{ // The statement takes the job only while nobody holds it. So the count is 1 // for the worker that got it, and 0 for every worker after that. public static function take(Core\Db\Connection $db, int $job): bool { var $write = $db->execute( 'update jobs set taken = ? where id = ? and taken = ?', [true, $job, false], ); return $write->affected() == 1; }}
// This prints `first worker: yes` and `second worker: no`.echo 'first worker: ', Worker::take($db, 1) ? 'yes' : 'no', "\n";echo 'second worker: ', Worker::take($db, 1) ? 'yes' : 'no', "\n";
$db->close();first worker: yessecond worker: no