mysqli_num_rows 返回 1 无论如何

2021-12-25 00:00:00 php mysqli

当我在 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(*) 你即使计数为零也总是返回一行.

你要么:

  1. 想去掉count(*)然后使用mysqli_num_rows()
  2. 获取count(*)
  3. 的结果
  1. Want to remove the count(*) and then use mysqli_num_rows() or
  2. Get the result of count(*)

.

$row = mysqli_fetch_assoc($query);
echo $row['COUNT(`user_id`)'];

相关文章