tags:

views:

117

answers:

2

This is a very simple example:

I have a table with two columns, id and password. I want to know when an update was done successfully, with success being defined as applied to an existing row.

PHP seems to only offer mysql_affeted_rows(), which doesn't differentiate between 0 rows affected, and 0 rows updated. http://php.net/manual/en/function.mysql-affected-rows.php This is awkward because the return value is dependent on the state of rows at the time of the transaction. In Postgresql you can get it both ways, UPDATE foo SET bar = NULL, will return the amount of rows updated in foo; whereas, UPDATE foo SET bar = NULL WHERE bar IS NOT NULL will return the amount of rows affected, by limiting the update.

Don't bother suggesting a previous SELECT, or a PHP counter var, that assumes there is a row. I want to know of clever ways to get the Pg functionality, without redundant queries.

+1  A: 

mysql_affected_rows will always return the number of rows updated by an UPDATE.

In other words, it will return the same number of rows for UPDATE foo SET bar = NULL as for UPDATE foo SET bar = NULL WHERE bar IS NOT NULL given the same starting data.

Or at least that's the way it's documented on the manual page you linked to, under the Return Values heading.

R. Bemrose
When using UPDATE, MySQL will not update columns where the new value is the same as the old value. This creates the possibility that mysql_affected_rows() may not actually equal the number of rows matched, only the number of rows that were literally affected by the query.
OMG Ponies
OMG Ponies: Exactly, that's the documentation I was referring to. Both queries have the effect of changing all non-null values in that column to null, so both should return the same number of affected rows.
R. Bemrose
+1  A: 

Does the mysqli function num_rows fits your needs ?

Arno
This would have been my answer.
Reynolds
I automatically assumed `mysqli_num_rows` has the same quirk as `mysql_num_rows`... that it only works for `SELECT` queries.
R. Bemrose