Php Database Class prepare error

I'm getting the following error and can't seem to find out what the problem is:

Fatal error: Call to a member function prepare() on a non-object in /home2/nzrod/public_html/it6261/projectc/database.class.php on line 42

This is the database.class.php file:
<?php
class Database{
    private $host      = DB_HOST;
    private $user      = DB_USER;
    private $pass      = DB_PASS;
    private $dbname    = DB_NAME;

    private $stmt;
    private $dbh;
    private $error;


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

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

    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);
    }

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

    public function resultset(){
        $this->execute();
        return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
    }

    public function single(){
        $this->execute();
        return $this->stmt->fetch(PDO::FETCH_ASSOC);
    }

    public function rowCount(){
        return $this->stmt->rowCount();
    }

    public function lastInsertId(){
        return $this->dbh->lastInsertId();
    }

    public function beginTransaction(){
        return $this->dbh->beginTransaction();
    }

    public function endTransaction(){
        return $this->dbh->commit();
    }

    public function cancelTransaction(){
        return $this->dbh->rollBack();
    }

    public function debugDumpParams(){
        return $this->stmt->debugDumpParams();
    }



}
?>

Open in new window


This is the file testingDatabase.php that calls the database class:
<?php

require 'database.class.php';
define("DB_HOST", "localhost");
define("DB_USER", "user");
define("DB_PASS", "password");
define("DB_NAME", "database");

$database = new Database;

$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();

echo $database->lastInsertId();


$database->beginTransaction();

$database->query('INSERT INTO mytable (FName, LName, Age, Gender) VALUES (:fname, :lname, :age, :gender)');

$database->bind(':fname', 'Jenny');
$database->bind(':lname', 'Smith');
$database->bind(':age', '23');
$database->bind(':gender', 'female');
$database->execute();
$database->bind(':fname', 'Jilly');
$database->bind(':lname', 'Smith');
$database->bind(':age', '25');
$database->bind(':gender', 'female');

$database->execute();

echo $database->lastInsertId();

$database->endTransaction();


$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE FName = :fname');

$database->bind(':fname', 'John');

$row = $database->single();

echo "<pre>";
print_r($row);
echo "</pre>";


$database->query('SELECT FName, LName, Age, Gender FROM mytable WHERE LName = :lname');

$database->bind(':lname', 'Smith');

$rows = $database->resultset();

echo "<pre>";
print_r($rows);
echo "</pre>";

echo $database->rowCount();
?>

Open in new window


Any help in identifying the reason for the error would be much appreciated...

Cheers!
Rodric MacOliverResearcherAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dave BaldwinFixer of ProblemsCommented:
I copied your code, created a database table to match, and changed only the database connection info and it worked perfectly.  I'm using PHP 5.3.28.
0
Rodric MacOliverResearcherAuthor Commented:
Dave Baldwin, you didn't get the following error?

Fatal error: Call to a member function prepare() on a non-object in /home2/nzrod/public_html/it6261/projectc/database.class.php on line 42

I don't know what could be the issue then...
0
Dave BaldwinFixer of ProblemsCommented:
Nope, no error at all.  I suspect you have more code than that.  I see no 'boolean' type variables in the code that you posted.  Try copying your code above into a separate directory and try it.
0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Rodric MacOliverResearcherAuthor Commented:
No I don't really! Its on its own directory already...
I'm using PHP 5.4.

Could it be anything else?
0
Dave BaldwinFixer of ProblemsCommented:
Tried it on PHP 5.4.34 and it still works perfectly.  lastInsertId(); only works if you have an 'autoincrement' id field.  I added 'error_reporting(E_ALL);' to the top of the file and then added date_default_timezone_set('America/Los_Angeles'); because PHP 5.4 will whine if you don't.  Like this.
<?php
error_reporting(E_ALL);
date_default_timezone_set('America/Los_Angeles');

require 'database.class.php';

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rodric MacOliverResearcherAuthor Commented:
Added the lines and for whatever reason still got the freaking error bellow:

Fatal error: Call to a member function prepare() on a non-object in /home2/nzrod/public_html/it6261/projectc/database.class.php on line 42

I'm thinking what else could be the error to show on my server and not happen with yours...
I have no idea!
0
Dave BaldwinFixer of ProblemsCommented:
Yes, I don't know either.
0
Rodric MacOliverResearcherAuthor Commented:
It helped me find the error, which was on the variables.
0
Dave BaldwinFixer of ProblemsCommented:
Glad you found it.  Thanks for the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.