mysqli调用成员函数()错误

时间:2017-06-21 07:20:27

标签: php mysql mysqli

所以我试图使用 MySQLi ,但是,我不断抛出错误

  

致命错误:在null中调用成员函数query()   第78行/home/u740966689/public_html/dashboard/API/infFunctions.php

我不确定为什么会这样......我读了一些其他问题,并改变了一些事情,例如我如何连接到数据库,但我仍然得到错误。

我的代码:

文件1:

require 'infFunctions.php';
$DatabaseHost   = '***';
$DatabaseUser   = '***';
$DatabasePass   = '***';
$DatabaseName   = '***';
$mysqli         =  new mysqli("$DatabaseHost","$DatabaseUser","$DatabasePass","u740966689_site") or die ("infAPI | Could not connect to the database");

if (isset($_GET['Type'])){
    $Type = $_GET['Type'];
    if ($Type == 'Register') {
        $Response = Signup($_GET['name'],$_GET['password'],$_GET['email']);
        if ($Response == '1') {
            $resp = 'success';
        } else if ($Response == '2') {
            $resp = 'error_one';
        } else if ($Response == '3') {
            $resp = 'error_two';
        } else {
            $resp = 'error_three';
        }
        echo $resp;
    }
}

文件2:

function Signup($Username, $Password, $Email){
    $UserArray = $mysqli->query("SELECT * FROM infWebAccounts WHERE Username='$Username'");
    $UserArray2 = $mysqli->query("SELECT * FROM infPendingAccounts WHERE Username='$Username'");
     if (mysqli_num_rows($UserArray) == 0 AND mysqli_num_rows($UserArray2) == 0){
          $Password   = hash("sha512",$Password);
          $Query      = $mysqli->query("INSERT INTO infPendingAccounts (Username,Password,Email,Verified) VALUES ('$Username','$Password','$Email',N')");
          return '1';
       }elseif (mysqli_num_rows($UserArray) > 0 ){
          return '2';
       }else{
          return '3';
       }
}

3 个答案:

答案 0 :(得分:0)

$ mysqli对象是在全局范围内创建的,但您尝试在函数内部使用它。这违反了PHP的范围规则,其中子范围无法隐式访问父范围中的状态。

PHP Scoping Rules

您可以使用global来明确表示您想要访问全局范围内的变量(通常这是一个坏主意,应该避免,因为它可能会导致意大利面条代码),或者在函数中添加一个参数,允许你传递mysqli对象。

答案 1 :(得分:0)

如果要使用全局变量,则必须将其添加到函数中。

global $mysqli; - 函数

的开头轻松添加Signup()
function Signup($Username, $Password, $Email)
{
    global $mysqli;

    $UserArray = $mysqli->query("SELECT * FROM infWebAccounts WHERE Username='$Username'");
    $UserArray2 = $mysqli->query("SELECT * FROM infPendingAccounts WHERE Username='$Username'");

    .
    . 
    .

查看PHP: Variable scope

  

在PHP中,如果要在函数中使用全局变量,则必须在函数内声明全局变量。

答案 2 :(得分:0)

问题在于PHP变量范围。在引用$ mysqli变量之前,在Signup()函数中添加 global $ mysqli

function Signup($Username, $Password, $Email){

        global $mysqli; // Add this line

$UserArray = $mysqli->query("SELECT * FROM infWebAccounts WHERE Username='$Username'");
        $UserArray2 = $mysqli->query("SELECT * FROM infPendingAccounts WHERE Username='$Username'");
        if (mysqli_num_rows($UserArray) == 0 AND mysqli_num_rows($UserArray2) == 0){
            $Password   = hash("sha512",$Password);
            $Query      = $mysqli->query("INSERT INTO infPendingAccounts (Username,Password,Email,Verified) VALUES ('$Username','$Password','$Email',N')");
           // $rTech      =  new Roblox();
          //  $rTech -> DoLogin($rTech->password);
           // $rTech -> SendPM($rTech->GetUserID($Username),"RDS Owner Analytics Site Verification","Please goto the site and verify with : \n" . hash("sha512",$Username.$Password));
            return '1';
        }elseif (mysqli_num_rows($UserArray) > 0 ){
            return '2';
        }else{
            return '3';
        }
    }

或者您只需将连接变量作为SignUp函数

中的参数传递
function Signup($Username, $Password, $Email, $mysqli){

}

在此处详细了解变量范围:http://php.net/manual/en/language.variables.scope.php