函数不工作 - 错误:在null上调用成员函数query()

In first case is my mysqli connection

$mysqli = new mysqli(...);

In second case is my query

$mysqli->query(...);

I get error

Call to a member function query() on null

sample:

function first()
{
    $i = 1;
}

function second()
{
    global $i;
    return $i;
}
echo second();

Blank screen.

if the code like this

$i = 1;

function second()
{
    first();
    global $i;
    return $i;
}
echo second();

then it works, but I need the first case

In your "function first()", $i is declared as a local variable. It cannot be referenced from outside of the first() function. So, when you call "global $i", you are getting a "null" value because there are no global variables named $i .

In your second example, you declared your $i variable outside of functions - which makes it a global variable. So, when you call "global $i". You are referencing the $i variable that you set equal to 1. As a result, the value returned is "1" instead of "null".

Please refer to the PHP docs here for more information on local and global variables. It contains an example EXACTLY like yours. http://php.net/manual/en/language.variables.scope.php