-1

I am getting this error Fatal error: Call to a member function prepare() on a non-object in login/include/DB_Functions.php on line 71. The line in question is $result = $db->prepare('SELECT count(*) FROM users WHERE email = :email');

INDEX

<?php
require_once 'include/DB_Functions.php';
    $db = new DB_Functions();
if ($db->isUserExisted($email)) {
            // user is already existed - error response
            $response["error"] = 2;
            $response["error_msg"] = "User already existed";
            echo json_encode($response);
        } else {
            // store user
            $user = $db->storeUser($name, $email, $flat, $admin, $dummy, $password);
}
?>

CONFIG

<?php


$config['db'] = array(
    'host'     => 'localhost',
    'username' => 'z',
    'password' => 'y',
    'dbname'   => 'x'

);
?>

DB CONNECT

<?php
class DB_Connect {

    // constructor
    function __construct() {

    }

    // destructor
    function __destruct() {
        // $this->close();
    }





    // Connecting to database
    public function connect() {
        require_once 'include/config.php';

        $db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']);
       $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return $db;
    }

    // Closing database connection
    public function close() {
        mysql_close();
    }

}

?>

DB FUNCTIONS

<?php

class DB_Functions {

    // constructor
    function __construct() {
        require_once 'DB_Connect.php';
        // connecting to database


    }

    // destructor
    function __destruct() {

    }
  public function isUserExisted($email) {


        $result = $db->prepare('SELECT count(*) FROM users WHERE email = :email'); 
$result->execute(array(   ':email'    => $email)); 
$number_of_rows = $result->fetchColumn(); 

        if ($number_of_rows > 0) {
            // user existed 
            return true;
        } else {
            // user not existed
            return false;
        }
    }
 public function storeUser($name, $email, $flat, $admin, $dummy, $password) {
        $uuid = uniqid('', true);
        $hash = $this->hashSSHA($password);
        $encrypted_password = $hash["encrypted"]; // encrypted password
        $salt = $hash["salt"]; // salt

 $result =  $db->prepare('INSERT INTO users(unique_id, name, email, flat, admin, dummy, encrypted_password, salt, created_at) VALUES(:uuid, :name, :email, :flat, :admin, :dummy, :encrypted_password, :salt, NOW())');
 $result->execute(array(':unique_id' => $uuid, ':name' => $name, ':email' =>$email, ':flat' =>$flat, ':admin' =>$admin, ':dummy' =>$dummy, ':encrypted_password' =>$encrypted_password, ':salt' =>$salt, ':created_at' => NOW()));

        if ($result) {
            // get user details 
            $uid = mysql_insert_id(); // last inserted id
            $result = mysql_query("SELECT * FROM users WHERE uid = $uid");
            // return user details
            return mysql_fetch_array($result);
        } else {
            return false;
        }
    }
}
?>
user3860911
  • 139
  • 1
  • 2
  • 7
  • You're mixing MySQL APIs, most likely the reason. They do **not** mix. Edit: No wait, it **is** the reason. – Funk Forty Niner Jul 26 '14 at 16:22
  • In the part where it gives the error it doesn't mix PDO with MySql. It fails first at `public function isUserExisted($email) `. It didn't get to the part that actually mixed the APIs. If I am wrong it would help if you were to tell me where it does the mix. Also marking the question as duplicate didn't help at all since that thing is super general. – user3860911 Jul 26 '14 at 16:29
  • You're mixing PDO prepared statements and `mysql_` functions. Or, what is it that I'm missing here or not grasping? – Funk Forty Niner Jul 26 '14 at 16:34
  • You could mark it as duplicate and post a link to a computer science degree or Wikipedia. – user3860911 Jul 26 '14 at 16:34
  • I marked/voted it as something else. [Gordon](https://stackoverflow.com/users/208809/gordon) has precedence on my vote and was marked/closed as such. So ask him instead. – Funk Forty Niner Jul 26 '14 at 16:35

1 Answers1

1

This is really bad code/design for starters, but a quick fix would be to add global $db; as the first line in your function.

A better solution would be to make $db a property of your DBFunctions class and use $this->db to refer to it.

Also, just a heads up - mysql_close and PDO do not mix. Don't try this.

David Xu
  • 5,555
  • 3
  • 28
  • 50
  • Do look at the entire code. – Funk Forty Niner Jul 26 '14 at 16:23
  • *"Also, just a heads up - mysql_close and PDO do not mix. Don't try this."* - Including `mysql_insert_id()` and `mysql_query` and `mysql_fetch_array` etc. etc. I doubt very much that it is answerable/salvageable. The OP will need to completely rebuild. – Funk Forty Niner Jul 26 '14 at 16:29