[thelist] mysql and php tip
Matt Warden
mwarden at gmail.com
Tue Jun 28 16:26:01 CDT 2005
<tip>
Often we are creating a form that has the purpose of editing record(s)
in the database. If you use UPDATE in SQL to update the record and
then use mysql_affected_rows to verify the update, you can run into
problems. I was reminded of this issue yesterday when
mysql_affected_rows() was returning zero after hitting 'submit' on the
form without changing anything. I pasted the SQL into the mysql
console and received something like this:
mysql> update euser set user_id=user_id where user_id=1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
This immediately refreshed my memory about how mysql_rows_affected()
returned the number of rows altered, in this instance. What I want to
know is the number of rows *matched*, to verify that something didn't
go wrong with the where clause, for instance (in this case, it is a
simple where clause, but it is often not).
Enter mysql_matched_rows():
http://mwarden.f2o.org/sandbox/viewsource.php?linemode=0&f=mysql_matched_rows.php
This will return the number of rows matched, or false (which will
usually happen if you try to call the function on a query that does
not update, e.g. SELECT).
</tip>
--
Matt Warden
Miami University
Oxford, OH, USA
http://mattwarden.com
This email proudly and graciously contributes to entropy.
More information about the thelist
mailing list