-2

So I am attempting to use MySQLi, however, I keep getting thrown the error

Fatal error: Call to a member function query() on null in /home/u740966689/public_html/dashboard/API/infFunctions.php on line 78

I'm unsure why this is... I read up on some other questions and changed a few things such as how I connect to the database but I still get the error.

MY CODE:

File 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;
    }
}

file 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 Answers3

0

The $mysqli object is created in the global scope but you're trying to use it inside a function. This is in violation of PHP's scoping rules, where a child scope can't implicitly access state in a parent scope.

PHP Scoping Rules

You can work around it by either using global to explicitly indicate that you want to access a variable in the global scope (which is generally a bad idea and should be avoided as it can lead to spaghetti code), or add a parameter to your function that will allow you to pass the mysqli object in.

GordonM
  • 31,179
  • 15
  • 87
  • 129
0

If you want to use a global variable you have to add this to the function.

Easily add global $mysqli; at the beginning of the Signup()-function


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'");

    .
    . 
    .

Check out PHP: Variable scope

In PHP global variables must be declared global inside a function if they are going to be used in that function.

modsfabio
  • 1,097
  • 1
  • 13
  • 29
0

The problem is with PHP variable scoping. Add global $mysqli inside Signup() function before you refer to the $mysqli variable:

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';
        }
    }

or you can simply pass your connection variable as parameter in your SignUp function

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

}

Learn more about variable scoping here: http://php.net/manual/en/language.variables.scope.php

Sehdev
  • 5,486
  • 3
  • 11
  • 34