PDO和php-对非对象调用成员函数prepare()
我开始学习PDO,我还是一个PHP新手。我在做一个项目来增加我的知识,但我在第一个障碍上卡住了 我得到这个错误:在代码第37行的非对象上调用成员函数prepare()。这来自database.class.phpPDO和php-对非对象调用成员函数prepare(),php,mysql,pdo,Php,Mysql,Pdo,我开始学习PDO,我还是一个PHP新手。我在做一个项目来增加我的知识,但我在第一个障碍上卡住了 我得到这个错误:在代码第37行的非对象上调用成员函数prepare()。这来自database.class.php <?php // Include database class include 'config.php'; class Database{ private $host = DB_HOST; private $user = DB_USER; private $p
<?php
// Include database class
include 'config.php';
class Database{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
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 instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
// this variable holds the temporary statement
private $stmt;
// prepare our values to avoid SQL injection
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
// use switch to select the appropiate type for the value been passed
// $param = placeholder name e.g username, $value = myusername
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;
}
}
// run the binding process
$this->stmt->bindValue($param, $value, $type);
}
// execute the prepared statement
public function execute(){
return $this->stmt->execute();
}
// returns an array of the results, first we execute and then fetch the results
public function resultset(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
// same as resultset() except this returns a single result
public function result_single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_ASSOC);
}
// count the number of rows from the previous delete, update or insert statement
public function rowCount(){
return $this->stmt->rowCount();
}
// last insert id return the last id of the insert made, useful if you need to run a further
// query on the row you just inserted and need the id as the reference
public function lastInsertId(){
return $this->dbh->lastInsertId();
}
// transactions allow you to rollback if a set of queries fail but it also allows ensures
// we avoid errors due to users interaction while half of the queries are still being written
// the following code begins a transaction
public function beginTransaction(){
return $this->dbh->beginTransaction();
}
// end a transaction
public function endTransaction(){
return $this->dbh->commit();
}
// rollback a transaction
public function cancelTransaction(){
return $this->dbh->rollBack();
}
// this dumps the PDO information from the prepared statement for debug purposes
public function debugDumpParams(){
return $this->stmt->debugDumpParams();
}
}
?>
现在,我在index.php中运行这段代码的初始页面
<?php
// Include database connection class
include 'includes/database.class.php';
include 'includes/bug.class.php';
$database = new Database;
$bugs = new Bugs;
$bugs->find_all_rows();
echo "<pre>";
print_r($rows);
echo "</pre>";
echo "Number of rows: " . $bugs->rowCount() . "<br />";
?>
这是它将运行find_all_rows函数的页面,在该页面上有bugs.class.php文件
. "'"
有没有一个调试工具可以帮助我更好地跟踪这些类型的错误,或者这只是一个我对PDO不够熟悉而无法发现明显错误的例子?@papaja一针见血。PDO连接失败,因此您没有PDO对象来运行prepare方法 在我的脑海中,我想你错过了$dsn字符串的结束引号。您可能希望在$this->dbname之后和分号之前添加以下内容:
"mysql:host=$this->HOST;dbname=$this->DATABASE"
这是一个用双引号括起来的单引号。我使用以下语法创建DSN字符串:
class TestDatabase{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
public function __construct(){
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
}
无论如何,创建一个测试文件,以便您确切地知道问题是什么。测试文件应如下所示:
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 instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
注意,我们没有在try-catch块中运行PDO对象的实例化。虽然您在生产环境中永远不会这样做,但它对您的测试非常有用,因为它将抛出一个包含连接所有细节的致命异常
现在实例化测试类,并通过调试收到的错误继续。同样,它们将比前面的错误更详细,因为这将是一个未捕获的PDO异常 获取相同错误,无法使用上面编写的代码修复 这是一个简单的修复方法,因为在您的代码中,这就是PDO发送错误的原因:
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 instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}
我正在使用PHP5.5,如果有人有相同的错误,那么他将需要这个
在$dns之后,您需要添加$this->dbname。";";
只有这是PHP5.5的工作方式,而不是$this->dbname。"'";
$this->error
的值是多少?为什么不给类bug
它自己的数据库对象,这样您就可以确定它是否实例化了?(这是OOP的“定律”之一)这是一个服务器错误,所以它甚至没有加载索引页,我刚刚开始学习Pdo,所以我确信上面可能有很大的错误,我只是在寻找一个正确的方向,一旦我有了几个查询,我想我将能够从中找出其中的大部分,当你说给类Bug它自己的数据库对象时,你的意思是类Bug扩展了数据库吗?不幸的是,这不是一个代码审查网站,用来观察上面所有的大错误。您必须一步一步地编写代码,而不是一次编写完所有代码。分别调试每个部分。您收到的错误说明$this->dbh
不是对象,这很可能意味着$this->dbh=new PDO($dsn,$this->user,$this->pass,$options)代码>失败。由于您正在处理该异常,因此无法立即看到该异常。谢谢,它毕竟是由于连接,现在工作正常
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 instanace
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
}
// Catch any errors
catch(PDOException $e){
$this->error = $e->getMessage();
}
}