Check if a value exists in a column in mysql table
I 开发者_JAVA技巧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?
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.
I suggest you quickly learn SQL queries :)
SELECT * from accountinfo where username = 'something'
Just query for the username:
SELECT `username` FROM `accountinfo` WHERE `username` = :existing_username
-- or
SELECT `username` FROM `accountinfo` WHERE `username` LIKE :existing_username
精彩评论