0

trying to learn the ins and outs of PDO and I just got into this brick wall.

My current PDO class is the following:

class SimpleDatabase extends PDO {

const DB_HOST='localhost';
const DB_USER='claudio';
const DB_PASS='claudio';
const DB_NAME='simpledb';

private $dbh;
private $error;

private $stmt;

public function __construct(){
    // Set DSN
    $dsn = 'mysql:host=' . self::DB_HOST . ';dbname=' . self::DB_NAME;
    // Set options
    $options = array(
        PDO::ATTR_PERSISTENT    => true,
        PDO::ATTR_ERRMODE       => PDO::ERRMODE_EXCEPTION
    );
    // Create a new PDO instanace
    try{
        $this->dbh = new PDO($dsn, self::DB_USER, self::DB_PASS, $options);
    }
    // Catch any errors
    catch(PDOException $e){
        $this->error = $e->getMessage();
    }
}

public function __destruct(){
// Adding null connection
    $this->dbh = null;
    $this->isConnected = false;
}

// The query, prepare, bind and execute methods allows us to use prepared statements in our DB interactions

// Prepare
public function query($query){
    $this->stmt = $this->dbh->prepare($query);
}

    // Bind
public function bind($param, $value, $type = null){
    if (is_null($type)) {
        switch (true) {
            case is_int($value):
                $type = PDO::PARAM_INT;
                break;
            case is_bool($value):
                $type = PDO::PARAM_BOOL;
                break;
            case is_null($value):
                $type = PDO::PARAM_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
        }
    }
    $this->stmt->bindValue($param, $value, $type);
}

// Execute
public function execute(){
    return $this->stmt->execute();
}

I do have the rest of the functions but for this error in particular I don't think it's necessary to show you the rest.

Then I'm calling the function this way:

    $database = new SimpleDatabase();
$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');
$database->bind(':fname', 'John');
$database->bind(':lname', 'Smith');
$database->bind(':age', '24');
$database->bind(':gender', 'male');

$database->execute();

And I'm getting the following error: Fatal error: Call to a member function prepare() on null, and this happens when I call the prepare function. Any idea on why this is happening and how can I solve this problem?

6
  • Where's your bind() function? Commented Feb 23, 2015 at 22:11
  • Just so you know that you have misspelled the destructor ie __destuct() Commented Feb 23, 2015 at 22:16
  • @CláudioRibeiro you mean __destruct() ;-) Commented Feb 23, 2015 at 22:18
  • @Fred-ii- No, he had named it __destuct(), am I missing something? Commented Feb 23, 2015 at 22:19
  • @Cyclone Ooops.. that should have been aimed at the OP. Edited comment. Commented Feb 23, 2015 at 22:20

1 Answer 1

1

It looks like your code is "swallowing" the PDO Exception raised when the database connect is attempted, and fails.

Here:

    catch(PDOException $e){
        $this->error = $e->getMessage();
    }

If an exception is raised, you assign something to the error member. Cool. But likely $this->dbh is going to be null, if the connect fails.

But otherwise it looks as if your code just continues as if everything is fine.

It's as if your code is putting its pinky finger to the corner of its mouth, Dr.Evil style, and saying "I just assume everything will go to plan, what?"

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.