jimmyt1988 Posted December 8, 2009 Share Posted December 8, 2009 my code is: <?php $usernameFetch = mysql_query("select username from userRegistration where ($username==username)"); if ($usernameFetch){ echo "Username already taken"; } else{ mysql_query("insert into userInformation(firstName, lastName, day, month, year, country, emailAddress, username, password) values('$firstName','$lastName','$day','$month','$year','$country','$emailAddress','$username','$convertedPassword')"); } ?> I want it to say if the mysql query returns true then it should echo "already taken". However it doesn't. I'm completely missing something basic Quote Link to comment https://forums.phpfreaks.com/topic/184439-if-username-taken/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 8, 2009 Share Posted December 8, 2009 mysql_query() for a SELECT query returns either a FALSE value or a result resource. The FALSE value is returned if the query failed to execute. This is usually due to syntax errors or problems with the database. A result resource will be returned if the query executed without any errors. A query that matches zero rows in your table is a successful query and it returns a result resource. You must use mysql_num_rows to find out how many rows there are in the result set that is returned. Quote Link to comment https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973621 Share on other sites More sharing options...
jimmyt1988 Posted December 8, 2009 Author Share Posted December 8, 2009 Thanks a bunch! Quote Link to comment https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973626 Share on other sites More sharing options...
jimmyt1988 Posted December 8, 2009 Author Share Posted December 8, 2009 <?php $usernameFetch = mysql_query("select username from userRegistration where ($username==username)"); $usernameValidation = mysql_num_rows($usernameFetch); if ($usernameValidation>0){ echo "Username already taken"; } else{ mysql_query("insert into userInformation(firstName, lastName, day, month, year, country, emailAddress, username, password) values('$firstName','$lastName','$day','$month','$year','$country','$emailAddress','$username','$convertedPassword')"); } ?> Hm now what's wrong? Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in D:\****\index.php on line 17 Quote Link to comment https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973663 Share on other sites More sharing options...
PFMaBiSmAd Posted December 8, 2009 Share Posted December 8, 2009 You are getting a FALSE value from your mysql_query() because the query is failing. You need single-quotes around the $username variable in the query so that it will be a string value and you need to use only one = sign for the comparison in the query. Quote Link to comment https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973667 Share on other sites More sharing options...
premiso Posted December 8, 2009 Share Posted December 8, 2009 $usernameFetch = mysql_query("select username from userRegistration where username = '$username'") or trigger_error('Query Failed: ' . mysql_error()); You had an error in your Query, the above should work, for future testing I would suggest to use the trigger_error function so you can see errors as shown above. Quote Link to comment https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973668 Share on other sites More sharing options...
jimmyt1988 Posted December 8, 2009 Author Share Posted December 8, 2009 THANKS! the error thing is gnna help.. edit: I GOT IT TO WORK.. yipppeee, thanks guys!!!! Quote Link to comment https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973672 Share on other sites More sharing options...
premiso Posted December 8, 2009 Share Posted December 8, 2009 $usernameFetch = mysql_query("select username from userRegistration where username = '$username'") or trigger_error('Query Failed: ' . mysql_error()); You had an error in your Query, the above should work, for future testing I would suggest to use the trigger_error function so you can see errors as shown above. Add the or trigger_error part to your code as shown above and see what error MySQL is throwing so you can accurately fix it. Quote Link to comment https://forums.phpfreaks.com/topic/184439-if-username-taken/#findComment-973675 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.