note 94612 added to mysqli.affected-rows
| From: | oilpc at oilpc dot com | Date: | Sat, 14 Nov 2009 18:06:32 +0000 |
| Subject: | note 94612 added to mysqli.affected-rows | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162903@lists.php.net to get a copy of this message | ||
For "INSERT" or "UPDATE" statement for modifying data contained in one row of
one table I checked if number of affected rows equals 1 to determine success of the operation. It
works fine both for errors and false value of WHERE condition (that might be generated according to
specific application user acces privileges).
<?php
if ($mysqli->affected_rows==1){
echo "success";
}
else {
echo "fail";
}
?>
Checking if mysqli->affected_rows will equal -1 or not is not a good method of determining
success of "INSERT IGNORE" statements. Example: Ignoring duplicate key errors while
inserting some rows containing data provided by user only if they will match specified unique
constraint causes returning of -1 value by mysqli->affected_rows even if rows were inserted.
(checked on MySQL 5.0.85 linux and php 5.2.9-2 windows). However mysqli->sqlstate returns no
error if statement was executed successfully.
<?php
if ($mysqli->affected_rows!=-1){
echo "success";// for "INSERT IGNORE" statements will not occur if there
were any duplicate key errors ignored during execution of the query
}
else {
echo "fail";// "INSERT IGNORE" statements causing any duplicate key errors
(however ignored) lead to mysqli->affected_rows equal -1
}
// Example below works for "INSERT IGNORE" stattements, too
if ($mysqli->sqlstate=="00000"){
echo "success";
}
else {
echo "fail";
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 89.79.48.116
----
Manual Page -- http://www.php.net/manual/en/mysqli.affected-rows.php
Edit -- https://master.php.net/note/edit/94612
Del: integrated -- https://master.php.net/note/delete/94612/integrated
Del: useless -- https://master.php.net/note/delete/94612/useless
Del: bad code -- https://master.php.net/note/delete/94612/bad+code
Del: spam -- https://master.php.net/note/delete/94612/spam
Del: non-english -- https://master.php.net/note/delete/94612/non-english
Del: in docs -- https://master.php.net/note/delete/94612/in+docs
Del: other reasons-- https://master.php.net/note/delete/94612
Reject -- https://master.php.net/note/reject/94612
Search -- https://master.php.net/manage/user-notes.php