php 致命错误:在非对象上调用成员函数 query()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14822012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:01:44  来源:igfitidea点击:

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

phpmysqli

提问by Weinz

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

致命错误:在第 8 行的 /Applications/XAMPP/xamppfiles/htdocs/login.php 中的非对象上调用成员函数 query()

The line is this:

这条线是这样的:

$res = $mysqli->query("SELECT * FROM user WHERE user='$user' and password='$pw'");

This is login.php:

这是登录.php:

$user = $_POST['user'];
$pass = $_POST['pass'];
$pw = md5($pass); 
include_once('connect.php');

function check_login($user,$pw,&$result){
    $res = $mysqli->query("SELECT * FROM user WHERE user='$user' and password='$pw'");
    $cont = 0;
    while($row = $res->fetch_object()){
        $cont++;
        $result = $row;
    }
    if($cont == 1){
        return 1;
    }
    else{
        return 0;
    }
}

if(!isset($_SESSION['userid'])){
  if(isset($_POST['login'])){
    if(check_login($user,$pw,$result) == 1){
        session_start();
        $_SESSION['userid'] = $result->id_user;
        header("location:index.php?var=ok");
    }
    else{
        header('location:index.php?var=log');
    }
  }
}

And the code of connect.php :

和 connect.php 的代码:

$mysqli = new mysqli('localhost', 'root', 'pass', 'cms' );
if ($mysqli->connect_error) {
   die('Error de Conexión (' . $mysqli->connect_errno . ') '
        . $mysqli->connect_error);
 }

What could be the problem? Problems connecting the database?

可能是什么问题呢?连接数据库有问题?

回答by helmbert

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

这很可能是一个范围界定问题。这意味着$mysqli您在包含文件中定义的变量在check_login函数的作用域之外(即在该函数内部是未知的)。

You could try to get the $mysqlivariable from global scope with

您可以尝试$mysqli从全局范围获取变量

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 statementsto prevent this issue (or at least escape your input variables with a function like mysqli::real_escape_string).

编辑:哦,您还应该注意代码中的 SQL 注入漏洞。使用准备好的语句来防止这个问题(或者至少用一个函数来转义你的输入变量mysqli::real_escape_string)。

回答by Green Black

function check_login($user,$pw,&$result){
    global $mysqli;
    $res = $mysqli->query("SELECT * FROM user WHERE user='$user' and password='$pw'");
    $cont = 0;
    while($row = $re[...]

Mind the "global". This puts the var in the scope of your method. This is a quick and dirty solution, but will work in this case.

注意“全球”。这会将 var 置于您的方法范围内。这是一个快速而肮脏的解决方案,但在这种情况下会起作用。

回答by Tomasz Fija?kowski

variable $mysqli in function check_login is out of scope (it is declare outside the function) so it is null.

函数 check_login 中的变量 $mysqli 超出范围(它在函数外声明),因此它为空。

回答by Tucker

It looks like your $mysqli variable is not being set properly within the scope of your code thats executing the query. Can you confirm that $mysqli is indeed a mysqli object and not, say, set to null?

看起来您的 $mysqli 变量没有在执行查询的代码范围内正确设置。你能确认 $mysqli 确实是一个 mysqli 对象而不是,比如说,设置为 null?

You can check this by doing: echo print_r($mysqli);right before you call the ->query method in your code.

您可以通过执行以下操作来检查:echo print_r($mysqli);就在您在代码中调用 ->query 方法之前。

If it is not set properly, track the variable backward in your code until you see why

如果未正确设置,请在代码中向后跟踪变量,直到您明白原因

回答by Robot Boy

public function connectDB($DBServer, $DBUser, $DBPass, $DBName) {
            global $con;
            $con = new mysqli($DBServer, $DBUser, $DBPass, $DBName) or die ("Error occured");
            return $con;

and then pass $conin all the functions that you define or run wherever.

然后传入$con您在任何地方定义或运行的所有函数。