警告:mysqli_num_rows()期望参数1为mysqli_result,给定字符串

时间:2015-04-26 04:06:16

标签: php mysqli

Hello Stackoverflow社区,我目前正在自己​​学习PHP,并希望找到解决这个问题的一些帮助每当我尝试登录我的登录页面时,我都会收到此消息:

  

警告:mysqli_num_rows()期望参数1为mysqli_result,   第38行给出的字符串   $ numrows = mysqli_num_rows($ query);

<html>
    <form action='login.php' method='POST'>
        <table>
            <tr>
                <td>
                    Username:
                    <input type='text' name='username' value='<?php echo $username; ?>'>
                </td>
            </tr>
            <tr>
                <td>
                    Password:
                    <input type='password' name='password'>
                </td>
            </tr>
        </table>
        <p>
        <input type='submit' name='submit' value='Login'>
    </form>
</html>
<?php
    session_start();
    $username = $_POST['username'];
    $password = $_POST['password'];

    if ($username&&$password)
    {
        $dbc = mysqli_connect('webhost', 'admin', 'password', 'database')or die('Error connecting to MySQL server.');

        $query = "SELECT username, password FROM Users WHERE username ='$username' AND password = SHA('$password')";
        $numrows = mysqli_num_rows($query);  //The line where the error lies in
        $result = mysqli_query($dbc, $query) or die(mysqli_error($dbc));
        $numrows = mysqli_num_rows($result);

        if ($numrows!=0)
        {

            while ($row = mysqli_fetch_assoc($query))
            {
                $dbusername = $row['username'];
                $dbpassword = $row['password'];
            }

            if ($username==$dbusername&&md5($password)==$dbpassword)
            {
                echo "You're in! <a href='report.php'>Click here to enter!</a>";
                $_SESSION['username']=$dbusername;
                mysqli_close($dbc);
            }
            else
                echo "Incorrect password!";

        }
        else
            die("That user doesn't exist!");

    }
    else
        die("Please enter a Username and Password.");
    ?>

我的在线主持人显示错误消息,但我的PHP书籍似乎并没有涵盖我在本主题中应该做的事情。似乎期待一个字符串是,但我无法弄清楚如何使这个工作。

2 个答案:

答案 0 :(得分:0)

首先必须执行查询然后获取num记录

这是不正确的

$numrows = mysqli_num_rows($query);  //The line where the error lies in
$result = mysqli_query($dbc, $query)

并且正确

if ($result=mysqli_query($dbc,$query ))
{
  // Return the number of rows in result set
  $numrows =mysqli_num_rows($result);      
}

答案 1 :(得分:0)

这应该解决这个问题

$result = mysqli_query($dbc, $query); $numrows = mysqli_num_rows($result);

您必须先执行查询,然后执行该功能。