EchoFool Posted June 12, 2008 Share Posted June 12, 2008 I have a mysql function to check the query before it actually updated.... yet even tho the row was affected the check seems to fail... <?php $Update = mysql_query("UPDATE usertable SET Complete=2 WHERE UserID='{$_SESSION['Current_User']}' AND OtherID='$ID'") Or die(mysql_error()); If(mysql_affected_rows()>1){ Echo 'pass'; }Else{ Echo 'fail'; } ?> I always get Fail being echo'd even though when I check my database it did infact update to "2" for the complete field. Which leads to me being confused as to why the mysql_affected_rows function is not working. Any ideas? Hope you can help! Link to comment https://forums.phpfreaks.com/topic/109884-solved-help-with-function/ Share on other sites More sharing options...
jonsjava Posted June 12, 2008 Share Posted June 12, 2008 I'm not seeing you execute the query. I see you setting a variable, but not actually running the query. Link to comment https://forums.phpfreaks.com/topic/109884-solved-help-with-function/#findComment-563842 Share on other sites More sharing options...
rhodesa Posted June 12, 2008 Share Posted June 12, 2008 mysql_affected_rows() will equal 1, not be greater then it... If(mysql_affected_rows() >= 1){ Link to comment https://forums.phpfreaks.com/topic/109884-solved-help-with-function/#findComment-563845 Share on other sites More sharing options...
.josh Posted June 12, 2008 Share Posted June 12, 2008 also you need to pass the result source as an argument for that Link to comment https://forums.phpfreaks.com/topic/109884-solved-help-with-function/#findComment-563848 Share on other sites More sharing options...
.josh Posted June 12, 2008 Share Posted June 12, 2008 I'm not seeing you execute the query. I see you setting a variable, but not actually running the query. That does run the query. It assigns the result source to the variable. Link to comment https://forums.phpfreaks.com/topic/109884-solved-help-with-function/#findComment-563849 Share on other sites More sharing options...
rhodesa Posted June 12, 2008 Share Posted June 12, 2008 also you need to pass the result source as an argument for that that argument is optional. as long as you only have one db connection, you don't have to worry about it Link to comment https://forums.phpfreaks.com/topic/109884-solved-help-with-function/#findComment-563853 Share on other sites More sharing options...
.josh Posted June 12, 2008 Share Posted June 12, 2008 mysql_affected_rows() will equal 1, not be greater then it... If(mysql_affected_rows() >= 1){ well then I will point out that the call will return however many rows that were affected, and -1 if it fails. Link to comment https://forums.phpfreaks.com/topic/109884-solved-help-with-function/#findComment-563870 Share on other sites More sharing options...
EchoFool Posted June 12, 2008 Author Share Posted June 12, 2008 Oh right i see that makes sense now ! Ok i changed it to >= 1 and it works a treat! Thanks guys ! Link to comment https://forums.phpfreaks.com/topic/109884-solved-help-with-function/#findComment-563871 Share on other sites More sharing options...
.josh Posted June 12, 2008 Share Posted June 12, 2008 Oh right i see that makes sense now ! Ok i changed it to >= 1 and it works a treat! Thanks guys ! I just want to point out that your code can be misleading. It could have executed the query just fine but mysql_affected_rows() could return 0 if the update didn't actually change anything. If you're just looking to find out if your query executed properly, your script already does that in the first place. You executed the query and if it failed, your die statement would give you an error. Link to comment https://forums.phpfreaks.com/topic/109884-solved-help-with-function/#findComment-563876 Share on other sites More sharing options...
rhodesa Posted June 12, 2008 Share Posted June 12, 2008 Oh right i see that makes sense now ! Ok i changed it to >= 1 and it works a treat! Thanks guys ! I just want to point out that your code can be misleading. It could have executed the query just fine but mysql_affected_rows() could return 0 if the update didn't actually change anything. If you're just looking to find out if your query executed properly, your script already does that in the first place. You executed the query and if it failed, your die statement would give you an error. that is a good point Link to comment https://forums.phpfreaks.com/topic/109884-solved-help-with-function/#findComment-563886 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.