Ben Shelock
Ben Shelock

Reputation: 20993

Check If Username Exists

Whats the best way to check if a username exists in a MySQL table?

Upvotes: 2

Views: 2791

Answers (4)

Astra
Astra

Reputation: 11241

If you are trying to determine if a user exists in MySQL (i.e. a user name exists that can login to MySQL itself).

select user,host from mysql.user where user = 'username';

If you need to filter by host:

select user,host from mysql.user where user = 'username' and host = 'localhost';

These queries lets you see who has access to the MySQL database server and only is accessible if you are an administrator for a MySQL server.

Upvotes: 3

chaos
chaos

Reputation: 124355

SELECT 1 FROM `users` WHERE `username` = 'whatever' LIMIT 1

Upvotes: 2

Alistair Evans
Alistair Evans

Reputation: 36513

SELECT COUNT(*) as count FROM users WHERE username='whatever'

read out the first result row - if the 'count' column is > 0 then the username exists.

Alternatively, in php, you could use:

SELECT * FROM users WHERE username='whatever'

Then use mysql_num_rows (or the equivalent).

Upvotes: 5

Shoban
Shoban

Reputation: 23016

Create a stored procedure to do it. Its faster than running the SQL via your code. I cannot anything more than just comparing it with the saved data in your table. ;-)

Upvotes: 0

Related Questions