note 58687 added to function.mysql-affected-rows
| From: | relsqui at chiliahedron dot com | Date: | Fri, 11 Nov 2005 22:04:45 +0000 |
| Subject: | note 58687 added to function.mysql-affected-rows | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-98446@lists.php.net to get a copy of this message | ||
Here's a stupid mistake I hope you won't have to scratch your head about as long as I did:
if you want to make sure a query succeeded, DON'T just test like this:
<?php
// WRONG
if (mysql_affected_rows()) {
// Success!
} else {
// Failure.
}
?>
If the query succeeded but didn't change anything (e.g. an update query with values which are
the same as the ones already in the database), that will report an error without actually failing.
This is the right way to do it:
<?php
// RIGHT
if (mysql_affected_rows() != -1) {
// Success!
} else {
// Failure.
}
?>
Or, if you also want to know whether anything changed:
<?php
// ALSO RIGHT
if (mysql_affected_rows() != -1) {
// Success!
if (mysql_affected_rows() == 0) {
// . . . but nothing has changed.
}
} else {
// Failure.
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.mysql-affected-rows.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58687
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58687&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58687&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58687&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58687&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58687&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58687&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58687&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58687&report=yes
Search -- http://master.php.net/manage/user-notes.php