Hello all,
I'm making a registration page for my website. I need to check if a username already exists. What SQL syntax and/or function can do this?
3 replies to this topic
#1
Posted 21 May 2011 - 04:14 AM
|
|
|
#2
Posted 21 May 2011 - 08:14 AM
It really depends on how you're storing them, currently.
I usually do something like: SELECT COUNT(*) AS MYCOUNTER FROM USERS WHERE USER_NAME='new name';
0 means it's available, 1 means it isn't.
Other people will do a select * and look to see if records were returned
Some people do an insert and handle the duplicate value if a primary key violation occurs.
I usually do something like: SELECT COUNT(*) AS MYCOUNTER FROM USERS WHERE USER_NAME='new name';
0 means it's available, 1 means it isn't.
Other people will do a select * and look to see if records were returned
Some people do an insert and handle the duplicate value if a primary key violation occurs.
#3
Posted 21 May 2011 - 08:15 AM
It should be simple as selecting the count, i.e.
SELECT COUNT(username) FROM tbl_users WHERE username = 'John'
If count > 0: //User already existsYou could probably use mysql_num_rows without COUNT if you are using PHP if you require more information (i.e. exists, and was registered at a date, rather than just exists already)
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#4
Posted 21 August 2011 - 04:44 PM
You can do something like this : SELECT username FROM tlb_name WHERE username = '".$username."', then use mysql_num_rows($query). then do a condition if the row equals 1, it means the username exists, if not it will continue and store the new username.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









