php 在非对象上调用成员函数 query()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14573076/
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 07:33:30  来源:igfitidea点击:

Call to a member function query() on a non-object

phpmysqli

提问by Sinan Samet

I get this error:

我收到此错误:

Call to a member function query() on a non-object.

在非对象上调用成员函数 query()。

Also when I use other MySQLi functions it gives me the same error but for that function.

此外,当我使用其他 MySQLi 函数时,它会给我同样的错误,但对于该函数。

My connection.php:

我的connection.php

<?php
$mysqli = new mysqli("localhost","user","pass","db");
if(mysqli_connect_errno()){
  trigger_error('Connection failed: '.$mysqli->error);
}

?>

My function:

我的功能:

function form_year($find, $value, $from, $vehicletype){
    //Query setup
    $autoquery = "SELECT    
                        Year, 
                        Vehicle, 
                        Brand, 
                        Model, 
                        Type, 
                    FROM 
                        vehicle_tbl
                    WHERE
                        Vehicle = '".$vehicletype."'
                    GROUP BY
                        Year";
    $autoresult = $mysqli->query($autoquery);

    $search = array("%value%", "%options%");
    $rows = file_get_contents("tpl/options.html");
    while($row = $autoresult->fetch_assoc()){
        $replace = array($row[$value], $row[$value]);
        $tr .= str_replace($search, $replace, $rows);
    }
    return str_replace($find, $tr, $from);
}

Function and connection is included where the function is called

函数和连接包含在调用函数的地方

回答by Green Black

Quick/dirty fix:

快速/脏修复:

  function form_year($find, $value, $from, $vehicletype){
    //Query setup
    global $mysqli;
    $autoquery = "SELECT  [...]

回答by Mike Brant

Your $mysqliobject is in global scope, not in the scope of the function. You should pass the mysqliobject to the function as a parameter such that you have access to it. So something like this:

您的$mysqli对象在全局范围内,而不是在函数范围内。您应该将mysqli对象作为参数传递给函数,以便您可以访问它。所以像这样:

function form_year($find, $value, $from, $vehicletype, $db){
    ...
    $autoresult = $db->query($autoquery);
    ...
}

Note I would also add some validation in the function to make sure a proper mysqli object was passed, but that is not shown.

注意我还会在函数中添加一些验证以确保传递了正确的 mysqli 对象,但未显示。

回答by Samuel Teuber

You might want to try to delete the comma after Typeas this might be a syntax error for the MySQL string. Also try to echo $mysqli->errorand look what it says.

您可能想尝试删除逗号,Type因为这可能是 MySQL 字符串的语法错误。也试着回声$mysqli->error看看它说了什么。

回答by Rosme

You got to make sure that your $mysqli is available in scope(which is not the case in your function). A few solution available: global, passing in arguments, singleton. Personnaly I usually do singleton for my database connection, something like that:

您必须确保您的 $mysqli 在范围内可用(在您的函数中并非如此)。一些可用的解决方案:全局、传入参数、单例。我个人通常为我的数据库连接做单例,就像这样:

class DB {
private static $_instance;

public static function get() {
    if(!isset(self::$_instance)) {
        self::$_instance = new mysqli("localhost","user","pass","db");
        if(mysqli_connect_errno()) {
            trigger_error('Connection failed: '.self::$_instance->error);
        }
    }
    return self::$_instance;
}
}

//Calling

$autoresult = DB::get()->query($autoquery);