2011-08-12 29 views
2

我有一个名为accountinfo的表,其中有一行叫做username。检查mysql表中的列是否存在值

当我运行register.php我想检查用户名是否已经存在于mysql表中或没有。

任何人有任何想法?

+7

你到目前为止尝试过什么? – Rikesh

回答

0

我建议你快速了解SQL查询:)

SELECT * from accountinfo where username = 'something' 
0

的用户名只是查询:

SELECT `username` FROM `accountinfo` WHERE `username` = :existing_username 
-- or 
SELECT `username` FROM `accountinfo` WHERE `username` LIKE :existing_username 
10
SELECT count(*) AS num_user 
FROM accountinfo 
WHERE username = "your_user_name" 

,这将给你一个非零值,如果your_user_name中已经存在数据库。如果它不存在,那么你将得到零。

P.S.

如果您不想在数据库中使用重复的用户名,那么您最好在表中添加唯一性约束。

要添加唯一性约束,使用此查询 -

ALTER TABLE accountinfo ADD CONSTRAINTS unique_username UNIQUE (username); 

添加此唯一性约束将节省你很多麻烦。