PHP, MySQL - can you distinguish between rows matched and rows affected?
Posted
by Renesis
on Stack Overflow
See other posts from Stack Overflow
or by Renesis
Published on 2010-05-27T23:58:06Z
Indexed on
2010/05/28
0:01 UTC
Read the original article
Hit count: 162
I am trying to write a PHP-MySQL database processor that is somewhat intelligent. When this processor decides it needs to make an update, I want to report if it was really successful or not. I thought I could use mysql_affected_rows
...
// Example:
// After running query "UPDATE mytable SET name='Test' WHERE ID=1"
$result = mysql_affected_rows();
if ($result >= 1) { /* Success */ }
If, for example, there was no row with ID=1, then $result
would be 0.
However, it turns out that PHP's mysql_affected_rows
is the actual affected rows, and may be still be 0 if the row exists but name
was already "Test". (The PHP docs even say this is the case).
If I run this in the command line, I get the following meta information about the query:
Query OK, 0 rows affected (0.01 sec)
Rows matched: 1 Changed: 0 Warnings: 0
Is there any way for me to get that "Rows matched" value in PHP instead of the affected rows?
© Stack Overflow or respective owner