longtone Posted April 4, 2008 Share Posted April 4, 2008 I want to run a competition where the first person to click on a link after it becomes active wins. $result = mysql_query("SELECT*FROM table WHERE won = 1 AND id = '$id'") ; if (mysql_num_rows($result) = 0) { mysql_query("UPDATE table SET won = 1 WHERE id ='$id'" ); echo 'You're a winner!'; } else { echo 'Bye loser'; } should work, unless another entrant sneaks in between the two queries, checks that there is no winner and announces their win, leading to two or more winners. I hink I can work out some logic to prevent this, but is there a simple way, like combining the two quries into one that is executed instantly? Quote Link to comment https://forums.phpfreaks.com/topic/99475-solved-preventing-simultaneous-updates-in-mysql/ Share on other sites More sharing options...
BillyBoB Posted April 4, 2008 Share Posted April 4, 2008 include a timestamp with the database entrant and then if it becomes a problem and they think they won just tell them that they were milliseconds away and to deal with the loss Quote Link to comment https://forums.phpfreaks.com/topic/99475-solved-preventing-simultaneous-updates-in-mysql/#findComment-509005 Share on other sites More sharing options...
sasa Posted April 4, 2008 Share Posted April 4, 2008 mysql_num_rows($result) = 0 is newer true Quote Link to comment https://forums.phpfreaks.com/topic/99475-solved-preventing-simultaneous-updates-in-mysql/#findComment-509018 Share on other sites More sharing options...
PFMaBiSmAd Posted April 4, 2008 Share Posted April 4, 2008 What you need to do is check the won value in the WHERE clause, so the UPDATE will only happen if the value is zero. Then use mysql_affected_rows() to find out if the row was updated. mysql_query("UPDATE table SET won = 1 WHERE won = 0 AND id ='$id'" ); // I am guessing id is the "link" id if(mysql_affected_rows() == 1) { echo 'You're a winner!'; } else { echo 'Bye loser'; } Quote Link to comment https://forums.phpfreaks.com/topic/99475-solved-preventing-simultaneous-updates-in-mysql/#findComment-509028 Share on other sites More sharing options...
longtone Posted April 5, 2008 Author Share Posted April 5, 2008 mysql_num_rows($result) = 0 is newer true Oops! I mean == , one day I'll stop doing that. Quote Link to comment https://forums.phpfreaks.com/topic/99475-solved-preventing-simultaneous-updates-in-mysql/#findComment-510146 Share on other sites More sharing options...
longtone Posted April 5, 2008 Author Share Posted April 5, 2008 What you need to do is check the won value in the WHERE clause, so the UPDATE will only happen if the value is zero. Then use mysql_affected_rows() to find out if the row was updated. Thanks, that's perfect. Quote Link to comment https://forums.phpfreaks.com/topic/99475-solved-preventing-simultaneous-updates-in-mysql/#findComment-510148 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.