致命错误:在布尔值上调用成员函数close()

时间:2016-09-26 16:19:33

标签: php

您好我有同样的问题,但我很困惑,为什么它不起作用。

Fatal error: Call to a member function close() on boolean in includes\register_check.php on line 45

这是$ stmt3-> close();

        } else {
    if (strlen($_POST['username2']) < 2 || strlen($_POST['username2']) > 20) {
        echo "<h3>Username must be between 2 and 20 characters!</h3>";
    } else {
        $stmt = yasDB_select("SELECT `id` FROM `user` WHERE `username` LIKE '$username'");
        $stmt2 = yasDB_select("SELECT `id` FROM `user` WHERE `email` LIKE '$email'");
        if ($stmt->num_rows == 0 && $stmt2->num_rows == 0) {
            $stmt3 = yasDB_insert("INSERT INTO `user` (`username`, `password`, `repeatpassword`, `name`, `email`, `website`, `plays`, `points`, `date`) VALUES ('$username','$password','$repeatpassword','$name','$email','$website','$plays','$points', '$date')", false);
            if ($stmt3) {
                echo "<h3>Registered! You may now log in.</h3>";
            } else {
                $stmt3->close();
                echo "<h3>Registration failed!</h3>";
            }
        } else {
            $stmt->close();
            echo "<h3>Sorry, username or email exists. Please try again.</h3>";
        }
    }
}

1 个答案:

答案 0 :(得分:0)

        $stmt3 = yasDB_insert("INSERT INTO `user` (`username`, `password`, `repeatpassword`, `name`, `email`, `website`, `plays`, `points`, `date`) VALUES ('$username','$password','$repeatpassword','$name','$email','$website','$plays','$points', '$date')", false);
        if ($stmt3) {
            echo "<h3>Registered! You may now log in.</h3>";
        } else {
            $stmt3->close();
            echo "<h3>Registration failed!</h3>";
        }

应该是

        $stmt3 = yasDB_insert("INSERT INTO `user` (`username`, `password`, `repeatpassword`, `name`, `email`, `website`, `plays`, `points`, `date`) VALUES ('$username','$password','$repeatpassword','$name','$email','$website','$plays','$points', '$date')", false);
        if ($stmt3) {
            $stmt3->close();
            echo "<h3>Registered! You may now log in.</h3>";
        } else {
            // $stmt3 is falsey (null or false) here, so we cant call close on it.
            echo "<h3>Registration failed!</h3>";
        }

与else一样,$stmt3是假的(很可能是假的),这是当你试图关闭它时导致Call to a member function close() on boolean错误的原因。