-2

Here is the snippet of code that is causing the error.

public function storeUser($email, $password) {

    require_once 'include/user.config.inc.php';

    $uuid = uniqid('', true);
    //echo $uuid;
    $hash = $this->hashSSHA($password);
    //echo $hash;
    $encrypted_password = $hash["encrypted"]; // encrypted password
    //echo $encrypted_password;
    $salt = $hash["salt"]; // salt
    //echo $salt;


    $query = "INSERT INTO user_table (unique_id, email_id, encrypted_password, salt, created_at) VALUES ( :uuid, :email, :encrypted_password, :salt, NOW()) ";
    $query_params = array(
        ':uuid' => $uuid,
        ':email' => $email,
        ':encrypted_password' => $encrypted_password,
        ':salt' => $salt
    );
    try {

        //These two statements run the query against the database table.
        $stmt = $db -> prepare($query);
        $result = $stmt -> execute($query_params);

    } catch (PDOException $ex) {

        $response["success"] = 0;
        $response["message"] = "Database Error! Problem with first query!";
        die(json_encode($response));

    }

}

I am getting an error:

Notice: Undefined variable: db

Fatal error: Call to a member function prepare() on a non-object

It seems that $query and $query_params is causing the problem but I don't know why. It seems right to me.

Here's the other snippet of code

if ($db->isUserExisted($email)) {

             // user is already existed - error response
            $response["error"] = 2;
            $response["error_msg"] = "User already exist";
            echo json_encode($response);

        } else {

            $db->storeUser($email, $password);

        }
user2708296
  • 33
  • 1
  • 2
  • 10

2 Answers2

1

Taking a guess here...

require_once only includes the file once, ever*. You have probably required that file before elsewhere, it's not getting loaded again, your $db variable is nowhere to be found.

* In the current script execution.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • yes this was the problem but now I am getting this error "A session had already been started - ignoring session_start()" how do i end the previous session as i have two functions accessing the database on separate occasions. I added the code where the two functions are called – user2708296 Nov 02 '13 at 23:00
0

You never initialize your $db object.

You need something like the following before "$stmt = $db -> prepare($query);"

$db = new mydbclass();
Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248