我尝试使用与我的问题相关的其他类似链接来获取答案,但无法解决。其他问题引用有一个 PHP 类,并使用它。在我的例子中,我已经有了 PHP 类,但我仍然遇到同样的错误。
我根据以下链接的解释制作了这个类(class): culttt.com/2012/10/01/roll-your-own-pdo-php-class/
在我的例子中,我使用的是 MS SQL。
下面是database.class.php的代码
class Database{
private $dbtype = DB_TYPE;
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
if ($this->dbtype == 'mysql') {
$dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;
}
if ($this->dbtype == 'sqlsrv') {
$dsn = 'sqlsrv:server=' . $this->host . ';Database=' . $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();
}
}
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();
}
}
从其他文件 (phppdo.php) 执行以下 PHP 代码将在 Apache 服务器上反射(reflect)如下结果:
第一次尝试执行 phppdo.php 将反射(reflect)结果。
第二次尝试执行 phppdo.php 将反射(reflect)以下错误:
fatal error :在 C:\xampp\htdocs\sattest\admin\includes\html\database.class.php 中的非对象上调用成员函数 prepare() 第 45 行
第三次尝试执行 phppdo.php 将使 Apache 服务器崩溃并重新启动 Apache 服务。
第四次尝试显示结果与第一次尝试一样好。出于测试目的,一次又一次地执行此文件时会重复此循环。
// Include database class
include('database.class.php');
// Define configuration
define("DB_TYPE", "sqlsrv"); // Values allowed: sqlsrv, mysql, sqlite
define("DB_HOST", "localhost");
define("DB_USER", "dbuser");
define("DB_PASS", "password");
define("DB_NAME", "dbname");
$database = new Database();
$database->query('SELECT * FROM incidents WHERE incident_id = 1 ');
$database->execute();
$rows = $database->resultset();
echo "<pre>";
print_r($rows);
echo "</pre>";
以下是 Apache 错误日志:
[Mon Nov 03 09:35:27.883145 2014] [core:warn] [pid 14276:tid 256] AH00098: pid file C:/xampp/apache/logs/httpd.pid overwritten -- Unclean shutdown of previous Apache run?
[Mon Nov 03 09:35:28.796237 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00455: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 configured -- resuming normal operations
[Mon Nov 03 09:35:28.796237 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00456: Server built: Aug 18 2012 12:41:37
[Mon Nov 03 09:35:28.796237 2014] [core:notice] [pid 14276:tid 256] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
[Mon Nov 03 09:35:28.799237 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00418: Parent: Created child process 4528
[Mon Nov 03 09:35:30.490406 2014] [mpm_winnt:notice] [pid 4528:tid 268] AH00354: Child: Starting 150 worker threads.
[Mon Nov 03 09:35:36.041961 2014] [:error] [pid 4528:tid 1756] [client 127.0.0.1:63411] script 'C:/xampp/htdocs/sattest/nzta-forms/admin/pdodb-tutorial.php' not found or unable to stat
[Mon Nov 03 09:36:40.478404 2014] [:error] [pid 4528:tid 1716] [client 127.0.0.1:63418] PHP Fatal error: Call to a member function prepare() on a non-object in C:\\xampp\\htdocs\\sattest\\nzta-forms\\admin\\includes\\html\\database.class.php on line 45
[Mon Nov 03 09:36:44.453802 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00428: Parent: child process exited with status 3221225477 -- Restarting.
[Mon Nov 03 09:36:45.002856 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00455: Apache/2.4.3 (Win32) OpenSSL/1.0.1c PHP/5.4.7 configured -- resuming normal operations
[Mon Nov 03 09:36:45.002856 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00456: Server built: Aug 18 2012 12:41:37
[Mon Nov 03 09:36:45.002856 2014] [core:notice] [pid 14276:tid 256] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
[Mon Nov 03 09:36:45.004857 2014] [mpm_winnt:notice] [pid 14276:tid 256] AH00418: Parent: Created child process 14456
[Mon Nov 03 09:36:46.597016 2014] [mpm_winnt:notice] [pid 14456:tid 268] AH00354: Child: Starting 150 worker threads.
在此先感谢您的帮助。 我真的需要尽快解决这个问题。
~萨丁德
最佳答案
好吧,我正要发表评论,但它太大了......
那个教程很傻。首先,它包装(装饰)PDO 类,但删除了该类的大部分功能。此外,在构造函数中,异常被捕获并将消息保存到无法访问的私有(private)变量中。这意味着您无法正确调试。
无论如何...
Fatal error: Call to a member function prepare() on a non-object in C:\xampp\htdocs\sattest\admin\includes\html\database.class.php on line 45
这意味着 $dbh
不是对象。由于 $dbh
是 PDO 实例,因此创建所述对象时出现问题(也许脚本无法连接到数据库,谁知道呢,因为异常被抑制了)。
因此,执行以下操作...在 database.class.php 构造函数中替换为:
// 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();
}
用这个:
// Create a new PDO instanace
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
然后发布你得到的异常。异常应该是正在发生的事情的 self 解释。
关于PHP fatal error : Call to a member function prepare() on a non-object in admin\includes\html\database. class.php 第 45 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26704129/