note 129672 added to mysqli.affected-rows
| From: | mishell.mercer@gmail.com | Date: | Sat, 27 Jul 2024 01:11:32 +0000 |
| Subject: | note 129672 added to mysqli.affected-rows | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-221444@lists.php.net to get a copy of this message | ||
Do note that if you have turned off autocommit, and plan to do multiple actions before the commit,
$affected_rows only works per statement. This means if, for example, you want to run multiple INSERT
statements and tally all rows inserted after they are commited, a running counter will need to be
implemented.
// start the count
$count = 0;
// turn off autocommit
$mysqli->autocommit(FALSE);
$mysqli->begin_transaction;
// insert a couple items
$query = "
INSERT INTO users ('id','username','email')
VALUES (1, 'userguy', 'userguy@mail.com'), (2, 'some_user',
'some_user@ymail.com')
";
$mysqli->query($query);
echo $mysqli->affected_rows; // 2 - correct
$count += $mysqli->affected_rows; // add to the count
// insert one more
$query = "
INSERT INTO users ('id','username','email')
VALUES (3, 'anotherone', 'anotherone@mail.com')
";
$mysqli->query($query);
$count += $mysqli->affected_rows;
// insert one more
$query = "
INSERT INTO users ('id','username','email')
VALUES (4, 'thefourth', 'thefourth@gmail.com')
";
$mysqli->query($query);
$count += $mysqli->affected_rows;
// commit these statements to the database
$mysqli->commit();
echo $mysqli->affected_rows; // 1 - only counts the last statement run
echo "Actual count: ".$count; // 4
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 76.132.56.167)
----
Manual Page -- https://php.net/manual/en/mysqli.affected-rows.php
Edit -- https://main.php.net/note/edit/129672
Del: integrated -- https://main.php.net/note/delete/129672/integrated
Del: useless -- https://main.php.net/note/delete/129672/useless
Del: bad code -- https://main.php.net/note/delete/129672/bad+code
Del: spam -- https://main.php.net/note/delete/129672/spam
Del: non-english -- https://main.php.net/note/delete/129672/non-english
Del: in docs -- https://main.php.net/note/delete/129672/in+docs
Del: other reasons-- https://main.php.net/note/delete/129672
Reject -- https://main.php.net/note/reject/129672
Search -- https://main.php.net/manage/user-notes.php