PEAR:DB affectedRows() returns wrong value
am 01.12.2010 03:45:46 von Daevid VincentThis took me a while to figure out but I'm fairly confident that
http://pear.php.net/manual/en/package.database.db.db-common. affectedrows.ph
p
Returns the wrong number.
I know this package is old and depricated, but I thought I'd throw this out
to the ether so that if someone else is searching, they might find this
useful. Maybe they're stuck with this library on their servers as we are
(for now)...
If you execute an UPDATE query in mySQL, the first time (assuming it is
syntactically correct and all that crap) it will return you:
(root@localhost) [agis_core]> UPDATE `gcs_commissioning` SET
`aeromobile_vlan_338_cidr` = '29' WHERE id_gcs_commissioning = '160' LIMIT
1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Execute the EXACT same query again and you get this:
(root@localhost) [agis_core]> UPDATE `gcs_commissioning` SET
`aeromobile_vlan_338_cidr` = '29' WHERE id_gcs_commissioning = '160' LIMIT
1;
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
And for sanity, change it slightly and get the expected result:
(root@localhost) [agis_core]> UPDATE `gcs_commissioning` SET
`aeromobile_vlan_338_cidr` = '30' WHERE id_gcs_commissioning = '160' LIMIT
1;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
Note the "matched" vs. "changed" values.
NOW... PEAR:DB affectedRows() will return '1' in ALL cases above. This is
significant and I believe it is erroneously returning the "matched" value
and NOT the "changed" value.
Tools like SQLYog will also tell you the CORRECT value but in an
abbreviated form like this:
1 row(s) affected
Execution Time : 00:00:00:000
Transfer Time : 00:00:00:000
Total Time : 00:00:00:000
Another thing I noticed is that in our sql_query() wrapper [around PEAR:DB]
we recently prefixed all queries with comments so we could easily tell
where they were coming from in "mytop"
/* server_ip : page.php */ UPDATE mytable SET foo='bar' WHERE id=1;
This causes affectedRows() to ALWAYS return 0 !!! WTFF?? Why should a
COMMENT have ANY effect on PEAR:DB queries. mySQL handles them just fine.
The queries EXECUTE just fine too, it's just that .
Lame. There goes 4 hours of my life I'll never get back.
d
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php