lovephp Posted April 10, 2014 Share Posted April 10, 2014 (edited) Guys i want to limit registration but its not working. say if members table already has 5 rows then i want to show the error registration closed but somehow its now working as in data still getting inserted... //Check number of members if($login != '' || $password != '') { $qry = "SELECT COUNT(*) FROM members"; $result = mysql_query($qry); if($result) { if(mysql_num_rows($result) > 5) { $errmsg_arr[] = 'Cannot add more users'; $errflag = true; } @mysql_free_result($result); } else { die("Query failed"); } } Edited April 10, 2014 by lovephp Quote Link to comment https://forums.phpfreaks.com/topic/287671-trying-to-limit-insertion-into-db-but-not-working/ Share on other sites More sharing options...
Jacques1 Posted April 10, 2014 Share Posted April 10, 2014 Hi, a COUNT(*) query on the whole table always returns exactly 1 row. Your mysql_num_rows() check makes no sense. Counting the rows before the INSERT query is generally naive and doesn't work when there are multiple requests at the same time. Imagine this scenario: Your table is empty. Then 10 users try to register at the same time, so you count the number of rows. Since it's 0 at that time, you let everybody register. Now you suddenly have 10 users despite your limit of 5. Checks like this are not that simple. Right now, the only solution I could think of would be to put an exclusive lock on the entire table, then count the rows and then insert the row. But this is really, really ugly. Why do you need this weird limit? Quote Link to comment https://forums.phpfreaks.com/topic/287671-trying-to-limit-insertion-into-db-but-not-working/#findComment-1475665 Share on other sites More sharing options...
Solution lovephp Posted April 10, 2014 Author Solution Share Posted April 10, 2014 lol done. its actually funny everytime i come here asking for help but end up fixing it by myself most of the time did this and i got what i needed //Check number of members $query = "SELECT COUNT(*) AS Count FROM members"; $result = mysql_query($query); $result = mysql_fetch_assoc($result); $count = $result['Count']; if($count >= 5) { $errmsg_arr[] = 'We cannot accept more users right now!'; $errflag = true; } thanks mate appreciate your time Quote Link to comment https://forums.phpfreaks.com/topic/287671-trying-to-limit-insertion-into-db-but-not-working/#findComment-1475673 Share on other sites More sharing options...
Jacques1 Posted April 10, 2014 Share Posted April 10, 2014 Unfortunately, you seem to have only read the first half of my reply. This does not work. At all. It may seem to work if you have very low traffic and a lot of luck. But this will fall apart at the next opportunity. If that's good enough for you, well, I can't stop you from doing it. But if you care just a tiny bit about correct code, this is simply wrong. Quote Link to comment https://forums.phpfreaks.com/topic/287671-trying-to-limit-insertion-into-db-but-not-working/#findComment-1475675 Share on other sites More sharing options...
lovephp Posted April 10, 2014 Author Share Posted April 10, 2014 Unfortunately, you seem to have only read the first half of my reply. This does not work. At all. It may seem to work if you have very low traffic and a lot of luck. But this will fall apart at the next opportunity. If that's good enough for you, well, I can't stop you from doing it. But if you care just a tiny bit about correct code, this is simply wrong. oh mate its just a small script with not too much traffic only for a office use for 15 users not more. yes i read and understood what u wrote thanks alot for the right advise ill keep that in mind. cheers Quote Link to comment https://forums.phpfreaks.com/topic/287671-trying-to-limit-insertion-into-db-but-not-working/#findComment-1475677 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.