user870380
user870380

Reputation: 1099

Check if a value exists in a column in mysql table

I have a table called accountinfo with a row called username.

When i run the register.php i want to check if the username already exists in the mysql table or not.

Anyone have any ideas?

Upvotes: 3

Views: 31263

Answers (3)

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29186

SELECT count(*) AS num_user 
FROM   accountinfo 
WHERE  username = "your_user_name"

which will give you a non-zero value if your_user_name already exists in the database. If it doesn't exist, then you will get zero.

P.S.

If you don't want duplicate username in the database, then you better add uniqueness constraints in your table.

To add uniqueness constraints, use this query -

ALTER TABLE accountinfo ADD CONSTRAINTS unique_username UNIQUE (username);

Adding this uniqueness constraint will save you from a lot of troubles.

Upvotes: 11

Brian
Brian

Reputation: 8626

I suggest you quickly learn SQL queries :)

SELECT * from accountinfo where username = 'something'

Upvotes: 0

feeela
feeela

Reputation: 29932

Just query for the username:

SELECT `username` FROM `accountinfo` WHERE `username` = :existing_username
-- or
SELECT `username` FROM `accountinfo` WHERE `username` LIKE :existing_username

Upvotes: 0

Related Questions