检查数据库MySQL PHP中是否存在用户名 [英] Check if username already exists in database MySQL PHP

查看:578
本文介绍了检查数据库MySQL PHP中是否存在用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个registration.php页面,我做,我需要检查一个用户名是否已经在我的数据库,所以我不会有2个成员相同的名称...

I have a registration.php page I made and I need to check if a username is already in my database so that I won't have 2 members with the same name...

这是我如何将用户信息插入数据库(用户名,密码,注册日期等):

Here is how I am inserting the user info into the database (username, password, date of registration, etc.):

mysql_query("INSERT INTO UserDb (ID, TimeStamp, UserName, Password) VALUES ('$ipaddress', NOW(), '$user_username', '$user_password')");

如何检查UserName字段下的数据库UserDb以确保用户的变量$ user_username尚不存在?

How can I check that database "UserDb" under the "UserName" field to make sure the user's variable "$user_username" doesn't already exist?

推荐答案

您可以使数据库中的用户名字段成为主键或唯一,这保证了数据库中的用户名唯一性。

You can make the username field in the database a primary key or unique, which guarantees the username uniqueness in the database.

然后,如果您尝试插入已存在的用户名 mysql_query()函数将失败,返回FALSE

Then, if you try to insert an already existing username the mysql_query() function will fail, returning FALSE.

应该总是使用一个简单的select语句查询数据库中是否存在用户名:

Besides that, you should always query the database for the existence of the username, using a simple select statement:

SELECT username FROM table WHERE username ='$ php_username_var' ;

这篇关于检查数据库MySQL PHP中是否存在用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆