AdRock Posted March 14, 2008 Share Posted March 14, 2008 I am having problems with this if statement takign the wrong branch If the user wants to add a record, i query the database to see if they have less than 3 records in there. If they have, perform the insert, if they have 3, display the error message to them. I have one record in the database but i am getting shown the error I want to count the number of times the userid occurs in the database and that is what i am using to compare against but it is not working $result = mysql_query("SELECT COUNT(userid) FROM carshare WHERE userid='".$_SESSION['user_id']."'") or die (mysql_error()); $numrows = mysql_fetch_row($result); if($numrows < 3) { //query to enter form data into database $sql = mysql_query("INSERT INTO carshare (userid, seats_available, start_street, start_postcode, start_lat, start_long, end_street, end_postcode, end_lat, end_long, depart_time) VALUES('$userid', '$seats', '$s_street', '$s_postcode', '$s_lat', '$s_long', '$e_street', '$e_postcode', '$e_lat', '$e_long', '$depart'"); //if the query didn't complete display error message if(!$sql) { echo "There has been an error adding your carshare. Please contact the webmaster via contact page."; } } else { echo "<h2 class=\"errorhead\">Warning - You are only allowed to add 3 car shares!!</h2>"; } Quote Link to comment https://forums.phpfreaks.com/topic/96070-counting-the-number-of-occurences-in-database/ Share on other sites More sharing options...
Caesar Posted March 14, 2008 Share Posted March 14, 2008 <?php $numrows = mysql_num_rows($result); // Returns the number of records in the query ?> Fetch row does not work the same way as mysql_num_rows Quote Link to comment https://forums.phpfreaks.com/topic/96070-counting-the-number-of-occurences-in-database/#findComment-491821 Share on other sites More sharing options...
sasa Posted March 14, 2008 Share Posted March 14, 2008 change to if($numrows[0] < 3) { Quote Link to comment https://forums.phpfreaks.com/topic/96070-counting-the-number-of-occurences-in-database/#findComment-491954 Share on other sites More sharing options...
Stooney Posted March 14, 2008 Share Posted March 14, 2008 Another tip would be to replace if(!$sql) { with if(mysql_affected_rows()==0){ Your way isn't wrong or anything, but this is what I would do. Quote Link to comment https://forums.phpfreaks.com/topic/96070-counting-the-number-of-occurences-in-database/#findComment-491977 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.