php 检查用户名是否存在于数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26357597/
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
Check if username exists in a database
提问by MattClaff
pretty stuck on trying to prevent the user from registering if the username exists. Here is my code:
如果用户名存在,则一直试图阻止用户注册。这是我的代码:
include('../connection.php');
//get all the names and values that have been posted.
$username = $_POST['username'];
//check if username exists
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($con,$query);
if(mysql_num_rows($result)>=1)
{
echo"name already exists";
}
else
{ // excecute insert query
I have tried loads of other stuff to get it working but for some reason my code doesn't like me......
我已经尝试了很多其他的东西来让它工作,但由于某种原因我的代码不喜欢我......
回答by arif_suhail_123
change mysql_num_rows
to mysqli_num_rows
更改mysql_num_rows
为mysqli_num_rows
if(mysqli_num_rows($result)>=1)//You are mixing the mysql and mysqli, change this line of code
{
echo"name already exists";
}
else
{ //
use bind param or proplerly escape your value, before using it in query.
在查询中使用绑定参数或正确转义您的值之前。
回答by Jayakarthik Appasamy
change..
改变..
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($con,$query);
to
到
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysqli_query($con,$sql);
you specified query in $sql and executing in $query... also get input as..
您在 $sql 中指定查询并在 $query 中执行...也将输入作为..
$username = mysqli_real_escape_string($con, $_POST['username']);
for security reasons...
出于安全原因...
回答by Mad Angle
mysql_num_rows
is deprecated as of PHP 5.5.0
, and will be removed in the future
mysql_num_rows
已弃用PHP 5.5.0
,将来会被删除
So use alternative mysqli_num_rows
所以使用替代 mysqli_num_rows
回答by Hara Prasad
try this
include('../connection.php');
$username = $_POST['username'];
$sql = "SELECT * FROM tbl_Freelancers WHERE User_Username = '".$username."'";
$result = mysql_query($sql);
if(mysql_num_rows($result)!=0)
{
echo"name already exists";
}
else
{
echo ".........";
}
回答by Kalaivani Nair
alter your query to:
将您的查询更改为:
$sql= "SELECT * FROM tbl_Freelancers WHERE User_Username = '$username'";
$result=mysql_query($sql);
if(mysql_num_rows($result)!=0)
{
echo"name already exists";
}
else
{
// excecute insert query
}
回答by Emm Jk
your $query
variable has to be a second argument in mysqli_query , instead you used $sql
. so replace $sql
with $query in mysqli_query()
function and use mysqli
instead of mysql_num_rows()
您的$query
变量必须是 mysqli_query 中的第二个参数,而您使用$sql
. 所以$sql
在mysqli_query()
函数中替换为 $query并使用mysqli
而不是mysql_num_rows()