1

I'm trying to create a function in PHP that connects to SQL with the global $conn, and another which authenticates the user. However, referencing the SQL connection function in the second function isn't working.

Error:

Fatal error: Call to a member function query() on a non-object in /[...]/functions.php on line [see below]

SQLinfo.php contains valid database information, in the form of constants SQLurl, SQLuser, SQLpass & SQLdatabase.

Code:

  function SQLconnect(){
    require 'SQLinfo.php';
    global $conn;
      $conn = new mysqli(SQLurl,SQLuser,SQLpass,SQLdatabase);
    if($conn->connect_error){
      return $conn->connect_error;
    }else{
      return True;
    }
  }


  function isauthenticated($username,$token){
    if(empty($username) || empty($token)){
        return False;
    }else{
      SQLconnect();
        //error line:
        $result = $conn->query("SELECT * FROM `userdata` WHERE `username` = '".$username."' AND `lastid` = '".$token."'");
        if($result->num_rows == 1){
            return True;
        }else{
            return False;
        }
    $result->free;
    }
  }

I tried looking at this answer, however since the mysql extension is deprecated and the global has been defined I couldn't figure it out. I'd appreciate any help.

Community
  • 1
  • 1
f.w.
  • 38
  • 4

0 Answers0