此函数返回唯一用户的行数按预期返回1但是为什么在检查IF语句时是否=== 1(也尝试== 1)它返回true,即使用户没有存在?
function test($user) {
global $link;
$query = "SELECT COUNT('iD') FROM eif WHERE userName = '$user'";
$result = mysqli_query($link,$query) or die(mysqli_error($link)); //or die(mysql_error());
$return_rows = mysqli_num_rows($result);
echo $return_rows, " is the number of rows", '<br>'; //this displays "1 is the number of the rows"
return $return_rows;
}
//$user = "austin16";
if(test($user)===1){
echo "yes ",$user," does exist";
} else{ echo " no ",$user," does not exist";}
//if $user="austin316" the IF returns TRUE however if $user="austin" it also returns TRUE but austin does not exist
?>
非常感谢您一起来看看。
答案 0 :(得分:0)
您的查询将始终返回一行,COUNT()
将返回与您的WHERE
子句匹配的行数。您可以选择不是聚合函数的东西,也可以与返回的值进行比较。最简单的解决方法是将您的COUNT('iD')
更改为ID
,这样您的$query
行就会$query = "SELECT iD FROM eif WHERE userName = '$user'";
这将使您的$return_rows
变量返回正确的值。