不能从我的mysql数据库中选择

时间:2013-12-09 17:14:33

标签: php mysql

我正在尝试访问在我的数据库上加密的密码我一直得到这个错误,我不知道如何解决它,我是一个新的PHP所以:P

以下是错误消息:警告:mysqli_num_rows()要求参数1为mysqli_result,第18行的C:\ wamp \ BSP \ login.php中给出布尔值

感谢任何帮助! :)

PHP代码:

<?php
    include('connection.php');
    session_start();

    function cryptPsw($input, $rounds = 9) {
        $salt = "";
        $saltChars = array_merge(range('A','Z'), range('a','z'), range(0,9));

        for($i = 0; $i < 22; $i++) {
            $salt .= $saltChars[array_rand($saltChars)];
        }
        return crypt($input, sprintf('$2y$%02d$', $rounds) .$salt);
    }

    $username = $_POST['username'];
    $password = $_POST['password'];
    $uname = mysqli_query($con, 'SELECT ´username´ FROM ´users´ WHERE ´username´ = "$username"');

    if(mysqli_num_rows($uname) == 0) {
        echo '<script>console.log("Username not found!")</script>';
    }
    else {
        $psw = mysqli_query($con, 'SELECT ´password´ FROM ´users´ WHERE ´username´ = "$username"');
        $hashedPsw = cryptPsw($psw);
        if(crypt($password, $hashedPsw) == $hashedPsw) {
            echo '<script>console.log("You are now logged in")</script>';
        }
        else {
            echo '<script>console.log("Wrong password, try again!")</script>';
        }
    }
?>

html表单:

<div class="login-form">
    <h3>Login</h3>
    <form action="login.php" method="post">
        <input type="text" name="username" placeholder="Username"/>
        <input type="password" name="password" placeholder="Password"/>

        <span>
            <input type="checkbox" name="checkbox"/>
            <label for="checkbox">Remember</label>
        </span>

        <input type="submit" name="submit" value="Login"/>
    </form>
</div>

1 个答案:

答案 0 :(得分:0)

尝试使用如下方法,即删除前勾选´使用反引号或保持原样,如果您使用了任何保留字/ s或关键字/ s,在这种情况下,可以使用反引号

'SELECT username FROM users WHERE username = "$username" '

而不是

'SELECT ´username´ FROM ´users´ WHERE ´username´ = "$username"'