note 58687 deleted from function.mysql-affected-rows by mazzanet
| From: | mazzanet@php.net | Date: | Fri, 06 Jan 2006 10:31:34 +0000 |
| Subject: | note 58687 deleted from function.mysql-affected-rows by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-101298@lists.php.net to get a copy of this message | ||
Note Submitter: relsqui at chiliahedron dot com
----
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.
}
?>