mysqli_num_rows总是返回0

时间:2018-06-20 12:07:38

标签: php mysql

这里试图从数据库中获取用户名和密码,如果找到他们的结果,则重定向到某个页面,但是mysqli_num_rows总是返回0,我不知道为什么`

    if (isset($_POST['login'])) {


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

    $query = "SELECT * FROM `login` WHERE username = '$username' AND password = '$password'";
    $run  = mysqli_query($con,$query);
    if (mysqli_num_rows($run) == 1) {
        header("location: login.php");

    }}?>

` Here is my DB tables and columns

1 个答案:

答案 0 :(得分:0)

        <?php

        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "ornament";   // My local DB Name

        $conn = mysqli_connect($servername, $username, $password, $dbname);
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }

            $username = "test";   // $_POST["username"] value
            $password = "test";     // $_POST["password"] value
            $sql = "SELECT * FROM `ornament` WHERE username = '$username' AND password = '$password'";
            $result = mysqli_query($conn, $sql);

             if (mysqli_num_rows($result) == 1) {
                   echo "correct";

                } else {
                    echo "wrong";
                }



        mysqli_close($conn);
        ?> 

        </body>
        </html>

输出:

     correct

我的数据库名称和表名称相同,但是对我来说很好。

注意:请检查从POST获取信息时是否使用了正确的密码。

任何解决方案,祝您有美好的一天

谢谢

穆图