开发者

Check if a value exists in a column in mysql table

开发者 https://www.devze.com 2023-03-27 09:16 出处:网络
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.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消