master82 Posted June 12, 2006 Share Posted June 12, 2006 I've created a registration form for my site and started the process of validating the input (check for blanks, length, passwords match etc) but I've come across a problem when it comes to checking the database to see if the requested username is already in use.I've already included the connection info, from what I remember I have to do an IF(... SELECT and LIMIT or something like that.Could anyone show me how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/11803-username-check/ Share on other sites More sharing options...
Buyocat Posted June 12, 2006 Share Posted June 12, 2006 One way to check for a username already existing would be:SELECT user_id FROM user_table WHERE username = '$username'Make sure to replace the values in your table with mine, anyway if a user_id is returned then you know the username is taken, else you're safe to continue. Quote Link to comment https://forums.phpfreaks.com/topic/11803-username-check/#findComment-44728 Share on other sites More sharing options...
master82 Posted June 12, 2006 Author Share Posted June 12, 2006 Thanx but I need a way of automatically checking if it is present, hence why I mentioned the IF before (If its already taken then i'll die("username already taken") but if no match is found then the script will continue to add the new user to the database Quote Link to comment https://forums.phpfreaks.com/topic/11803-username-check/#findComment-44731 Share on other sites More sharing options...
Buyocat Posted June 12, 2006 Share Posted June 12, 2006 Could you elaborate on what you mean by automatic? Do you mean you want to check and execute the insert in a single query? Quote Link to comment https://forums.phpfreaks.com/topic/11803-username-check/#findComment-44734 Share on other sites More sharing options...
wildteen88 Posted June 12, 2006 Share Posted June 12, 2006 You'll want to use Buyocats query and the code will be like this:[code]//connect to database$sql = "SELECT user_id FROM user_table WHERE username = '$username' LIMIT 1";$result = mysql_query($sql);if(myql_num_rows($result) == 1){ echo "Username in use";}else{ //continue to register user}[/code]Also you might want to set the username field to be UNIQUE too. Quote Link to comment https://forums.phpfreaks.com/topic/11803-username-check/#findComment-44736 Share on other sites More sharing options...
master82 Posted June 12, 2006 Author Share Posted June 12, 2006 Thanks, thats just what I needed...if(myql_num_rows($result) == 1)sorry for the bad explination -> that is what I was after [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/11803-username-check/#findComment-44738 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.