php 检查mysql表的列中是否存在值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7037182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:51:49  来源:igfitidea点击:

Check if a value exists in a column in mysql table

phpmysql

提问by user870380

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

我有一个名为 accountinfo 的表,其中一行名为 username。

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

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

Anyone have any ideas?

谁有想法?

采纳答案by Brian

I suggest you quickly learn SQL queries :)

我建议你快速学习 SQL 查询 :)

SELECT * from accountinfo where username = 'something'

回答by MD Sayem Ahmed

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

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

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

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.

添加此唯一性约束将使您免于很多麻烦。

回答by feeela

Just query for the username:

只需查询用户名:

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