简体   繁体   中英

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?

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.

PS

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.

Just query for the username:

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

I suggest you quickly learn SQL queries:)

SELECT * from accountinfo where username = 'something'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM