...

View Full Version : mysql_affected_rows returning -1 instead of 0?



NancyJ
09-19-2009, 01:51 PM
I have a script that updates the availability flag on data tables whenever availability is altered, before doing this I wrote a script to process all the availability of all properties. Now I want to use this script to periodically check that the availability is up-to-date - ie that there are no other scripts changing availability that we didn't know about/forgot.

So my idea was to add up the results of mysql_affected_rows. If it was 0 that means the availability was still up-to-date. But it came to -1888

My calls look like


mysql_query('update owners_pricing_regular set available = 1 where pid='.$row['pid'].' and month = "'.$month.'" and year = "'.$row['year'].'"') or die(mysql_error());
$affected_rows += mysql_affected_rows();

Now mysql_affected_rows should return -1 if the query fails but the script should die if the query fails, so I would expect it to return 0 if the scripts are working.

hinch
09-19-2009, 03:05 PM
$ranquery = mysql_query('update owners_pricing_regular set available = 1 where pid='.$row['pid'].' and month = "'.$month.'" and year = "'.$row['year'].'"') or die(mysql_error());
$affected_rows += mysql_affected_rows($ranquery);

off top of head and with a 4 alarm hangover

bacterozoid
09-19-2009, 04:14 PM
mysql_affected_rows() doesn't take a resource as an argument - it takes a link identifier for your mysql connection, but it's not required. It automatically picks up the last link opened by mysql_connect().

http://us3.php.net/manual/en/function.mysql-affected-rows.php

MattF
09-19-2009, 09:20 PM
If I recall correctly, there doesn't need to be a matching record in the DB for that update to run successfully. If there isn't a match, there merely won't be an update, hence you'll be deducting one each time there is no match. Is that code within a loop?

ilight
09-19-2009, 10:25 PM
http://us3.php.net/manual/en/function.mysql-affected-rows.php
as u mentioned already.
update wont return negative values right?
So wat could be wrong?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum