Search code examples
phpmysqlfunctionpdoprepare

php fatal call to member function prepare() on non object


Been looking everywhere for an answer as to why my pdo prepare() function is giving me this error:

PHP Fatal error: Call to a member function prepare() on a non-object in /var/www/database.class.php on line 26

I have been looking at every post about this but none of them seem to help or even just clear up this error. I started here: http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/ a havent changed anything just trying to use it with POST data

this is my database.class.php:

<?php
class Database {
    private $host = "localhost";
    private $user = "nicholas";
    private $pass = "12345";
    private $dbname = "sstest";

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

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

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

    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_NULL;
                break;
            default:
                $type = PDO::PARAM_STR;
                break;
            }
        }
        $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();
    }


}

?>

and here is the php file using my database:

<?php
include 'database.class.php';

$id = $_POST["id"];
$fname = $_POST["fname"];
$lname = $_POST["lname"];

$database = new Database();

$sql = 'INSERT INTO sstest (id, fname, lname) VALUES (:id, :fname, :lname)';

$database->query($sql);

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

$database->execute();

echo $database->lastInsertId();
?>

I know the java code is sending the POST data, but i am confused as to why the prepare() function is saying $sql is a non-object. Any help is much appreciated, ive been working on this for 2 days now and am not able to get past the prepare() statement.


Solution

  • I think your problem is here:

    try {
        $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
    } catch (PDOException $e) {
        $this->error = $e->getMessage();    
    }
    

    Add this line to catch after the line you have there:

    var_dump($this->error);exit();