tags:

views:

40

answers:

3

How can I check if a user exists?

Im doing an installer for a mysql database, and I need to check if a user exits, if not create user, if yes delete user and create it again.

this so i can execute the script without worries.

thanks.

+1  A: 

MySQL stores user data in a table called user in a database named mysql (by default). The following query will return 1 if a user with the specified username exists, 0 otherwise.

SELECT EXISTS(SELECT 1 FROM mysql.user WHERE user = 'username')
Lauri Lehtinen
+2  A: 

If you're deleting the MySQL user anyways, then there's really no need to check if it exists first. MySQL won't throw any errors if there's nothing to delete:

DELETE FROM mysql.user WHERE User = 'username';
Matt
+1 for thinking one step ahead .. I like it
Lauri Lehtinen
A: 

Depending whether or not the original script is in PHP or not, you could use PHP to determine and take action based on all the information provided.

Nik