-Felix- Posted October 8, 2005 Share Posted October 8, 2005 When I a user registers a new account, he/she must choose a user name. Of course, I dont want two users to pick the same username, so what I need to know is how to search the MySQL table to find out if a specific user name has already been picked? For example, a user enters desired username: Bob So I need to search if there already is a user registered with "Bob" How do I do this? mysql_query("SEARCH.....") ???? Quote Link to comment Share on other sites More sharing options...
effigy Posted October 9, 2005 Share Posted October 9, 2005 if ( select id from table where name = '$name' yields rows ) { error } else { continue } Quote Link to comment Share on other sites More sharing options...
mysqlautobackup Posted October 10, 2005 Share Posted October 10, 2005 $dbh = mysql_connect('mysql_host:3306','username','password'); mysql_select_db('database'); $query = "SELECT * FROM users_table WHERE username='$username'"; $res = mysql_query($query, $dbh); $rows = mysql_num_rows($res); if ($rows > 0) { echo 'Error!'; } else { // continue to add the user } mysql_close($dbh); Quote Link to comment Share on other sites More sharing options...
ryanlwh Posted October 13, 2005 Share Posted October 13, 2005 why dont you just make username a UNIQUE field? Then just do a normal INSERT and check whether mysql_query is true or false. If false, then the username exists. Quote Link to comment 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.