Fatal error: Call to a member function query() on a non object

后端 未结 5 2070
夕颜
夕颜 2020-12-21 17:11

I\'ve got this error:

Fatal error: Call to a member function query() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/login.php on line 8

The line is

5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-21 18:03

    This is most likely a scoping issue. This means that the variable $mysqli that you define in your included file is outside of the check_login function's scope (i.e. is not known inside this function).

    You could try to get the $mysqli variable from global scope with

    function check_login($user,$pw,&$result){
        global $mysqli;
        $res = $mysqli->query("SELECT * FROM user WHERE user='$user' and password='$pw'");
        // ...
    

    Edit: Oh, and you should also mind the SQL injection vulnerabiliy in your code. Use prepared statements to prevent this issue (or at least escape your input variables with a function like mysqli::real_escape_string).

提交回复
热议问题