PHP用户名检查功能不起作用

时间:2013-11-22 11:23:33

标签: php

$con = mysqli_connect("localhost","root","","database");

function user_exists($username) {
$username = sanitize($username);
$query = "SELECT COUNT(user_id) FROM users WHERE username = '$username'";
$result = mysqli_query($con, $query);
return (mysql_result($result, 0) == 1) ? true : false;
}

我认为查询中存在错误,因为我总是会收到“Username is taken”错误,如果我插入现有用户名则无关紧要。

**解决了感谢**

1 个答案:

答案 0 :(得分:0)

试试这个

$query = "SELECT COUNT(user_id) as user_id FROM users WHERE `username` = '$username'";
$result = mysqli_query($con, $query);

mysql_result不会返回结果,它会发送错误,所以它总是给你真实。

使用下面的代码来获得结果

$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
if($row['user_id']){ return true; } else { return false; }