php致命调用非对象的成员函数prepare()

时间:2014-04-28 21:20:24

标签: php mysql function pdo prepare

一直在寻找答案,为什么我的pdo prepare()函数给我这个错误:

PHP致命错误:在第26行的/var/www/database.class.php中调用非对象的成员函数prepare()

我一直在查看有关此内容的所有帖子,但它们似乎都没有帮助甚至只是澄清此错误。我从这里开始:http://culttt.com/2012/10/01/roll-your-own-pdo-php-class/ 只是试图将它与POST数据一起使用时,没有改变任何东西

这是我的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();
    }


}

?>

这是使用我的数据库的php文件:

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

我知道java代码正在发送POST数据,但我很困惑为什么prepare()函数说$ sql是非对象。非常感谢任何帮助,我已经在这2天工作,我无法通过prepare()声明。

1 个答案:

答案 0 :(得分:1)

我认为你的问题在这里:

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

添加此行以捕获您在该行之后的行:

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