jjmil03
jjmil03

Reputation: 21

call to member function prepare error

I am having an issue executing the below code. I get an error: Fatal error: Call to a member function prepare() on null in C:\xampp\htdocs... everytime I run it. It errors out right when it is about to query the database for some reason.

I am creating a function to check a username and password, and if it matches, log the user in and so forth. What I am trying to do is feed the outer function a username and a password, then pass those variables to the inner function (checkUser) to retrieve the user and password. Once I have those in an array, I want to compare to see if they match. If they do, then I want to continue on (I left the rest out for simplicity's sake). I don't know why I am getting the error I am getting, especially since it won't even run the 3rd line in the CheckUser without a fatal error.

This is homework, fyi, cards on the table. Just trying to get past this part. Thanks for any help.

function isValidUser($username, $password){
     $checker = checkUser($username, $password);
     if ($checker[user_email] == $username && $checker[user_pwd] == $password ) {
         return TRUE;
     }

 }

function checkUser($username, $password) {
    global $db;
    $st = $db -> prepare('SELECT * FROM user WHERE user_email = ? and user_pwd = ?;');
    $st -> bindParam(1, $username);
    $st -> bindParam(2, $password);
    $st -> execute();
    return $st -> fetch(PDO::FETCH_ASSOC);
}

Upvotes: 0

Views: 109

Answers (1)

b3tac0d3
b3tac0d3

Reputation: 908

try this instead of global $db

$db = $GLOBALS['db'];

Upvotes: 1

Related Questions