mysqli_num_rows不管什么都返回1 [英] mysqli_num_rows returns 1 no matter what
本文介绍了mysqli_num_rows不管什么都返回1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我在phpMyAdmin中执行SQL搜索(用变量替换实际值)时,它返回正确的行号,但是当使用PHP返回此值时,无论如何它总是返回1.预先感谢.
When I do a SQL search in phpMyAdmin (substituting the variable for the actual value) it returns the correct row number but when using PHP to return this value it always returns 1 no matter what. Thanks in advance.
function user_exists($username) {
$link = mysqli_connect('localhost','root','','test');
$username = sanitize($username);
$query = mysqli_query($link, "SELECT COUNT(`user_id`) FROM `new_base` WHERE `username`='$username'");
$row_cnt = mysqli_num_rows($query);
echo $row_cnt;
mysqli_free_result($query);
mysqli_close($link);
}
推荐答案
当您使用COUNT(*)
时,即使计数为零,您也总是得到返回一行.
When you use COUNT(*)
you always get one row returned even if the count is zero.
您要么:
- 要删除
count(*)
,然后使用mysqli_num_rows()
或 - 获取
count(*)
的结果
- Want to remove the
count(*)
and then usemysqli_num_rows()
or - Get the result of
count(*)
.
$row = mysqli_fetch_assoc($query);
echo $row['COUNT(`user_id`)'];
这篇关于mysqli_num_rows不管什么都返回1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文