错误:mysqli_num_rows()期望参数1为mysqli_result

时间:2014-06-15 14:28:05

标签: php mysql

我是非常新的php和mysql方面的东西,我希望有人可以帮助我,我的代码返回错误:mysqli_num_rows()期望参数1是mysqli_result

这是我的代码

$username = $_POST['username'];
$password = $_POST['password'];

if(isset($username) && isset($password)) {

    $connect = mysqli_connect("localhost","root","") or die("Couldnt connect!");

    mysqli_select_db($connect,"phplogin") or die("Couldnt find DB.");

    $query = "SELECT * FROM users WHERE username={$username}";
    $numrows = mysqli_num_rows($query);

    echo $numrows;

如果有人可以帮助我并告诉我我做错了什么以及我如何能够回应出行数,我会非常欣赏它。

2 个答案:

答案 0 :(得分:0)

功能mysqli_num_rows需要结果,您给它string

在num_rows之前,您必须致电$result = mysqli_query($db_connect, $query)。然后,您可以使用mysqli_num_rows($result);计算记录。

答案 1 :(得分:0)

您必须先实际运行查询,然后才能看到有多少行:

$query = "SELECT * FROM users WHERE username={$username}";

// Run the query
$result = mysqli_query($connect, $query);

$numrows = 0;

if(!$result)
{
    $numrows = mysqli_num_rows($result);
}

如果你遇到困难,请记得在php.net网站上查找,它是帮助的金矿:http://www.php.net//manual/en/mysqli-result.num-rows.php