问题描述
我做了相当多的研究,疑难解答和搜索,试图解决我的问题,没有运气。所以这里是错误...
I have done quite a fair amount of research, troubleshooting, and searching in an attempt to solve my problem, with no luck. So here is the error...
调用非对象上的成员函数prepare()
Call to a member function prepare() on a non-object
生成此错误的代码在我的用户Auth类中,如下所示:
The code generating this error is in my user Auth class as follows...
$this->dbManager->db->prepare('INSERT INTO users (email, password, user_salt, is_admin) VALUES (:email, :password, :user_salt, :is_admin)');
dbManager来自$ db,我已经传递到我的类的__construct如下...
dbManager comes from $db which I have passed into the __construct of my class as follows...
public function __construct($db) {
$this->siteKey = 'notImportant';
$this->dbManager = $db;
}
$ db来源于我的sql类,我需要在一个bootstrap
$db originates from my sql class, which I require in one "bootstrap" php file that the index.php requires.
bootstrap.php代码如下...
The bootstrap.php code is as follows...
<?php
// Load firePHP library
//require_once('FirePHPCore/FirePHP.class.php');
ob_start();
//$firephp = FirePHP::getInstance(true);
// Load all the classes/libs...
require_once 'classes/sql.class.php';
require_once 'classes/auth.class.php';
$db = sql::getInstance();
session_start();
$auth = new Auth($db);
require_once 'lib/handlepost.php';
最后,我的sql.class.php代码如下...
And finally, my code for sql.class.php is as follows...
class sql {
public static $db = false;
private $database_host = '';
private $database_user = '';
private $database_pass = '';
private $database_db = '';
private function __construct() {
if (self::$db === false) {
$this -> connect();
}
return self::$db;
}
public static function getInstance() {
if (!self::$db) {
self::$db = new sql();
}
return self::$db;
}
private function connect() {
$dsn = $this -> database_type . ":dbname=" . $this -> database_db . ";host=" . $this -> database_host;
try {
self::$db = new PDO($dsn, $this -> database_user, $this -> database_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
self::$db -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
//print_r($e->errorInfo);
//echo 'Connection failed: ' . $e->getMessage();
}
} // End Connect
}
我是新的使用PDO,这导致我开始的一些混乱开始...
I am new to using PDO, which is causing me some confusion to begin with...
任何帮助得到这个工作与我在做什么的解释
Any help getting this to work with an explanation of what I am doing wrong would be extremely appreciated.
推荐答案
您看到的错误意味着 $ this-> dbManager-> db
不是一个对象。因此,尝试调用它上的方法( prepare
)会出现错误。
The error you're seeing means that $this->dbManager->db
is not an object. Thus trying to call a method on it (prepare
) gives you an error.
编辑:您现在的错误。
Sql :: $ db
是一个静态成员,这是一个财产。你不能这样做。我建议将您的类 sql
上的静态成员更改为常规属性。例如:从 public static $ db = false;
中删除 static
用 $ this-> db
替换所有 self :: $ db
,并重写 getInstance
方法:
Sql::$db
is a static member, but you're accessing it as if it was a property. You can't do that. I suggest changing the static member on your class sql
to a regular property. E.g.: remove static
from public static $db = false;
. Replace all self::$db
with $this->db
and rewrite the getInstance
method to:
public static function getInstance() {
if (!self::$instance) {
self::$instance = new self();
}
return self::$instance;
}
基本上,你的错误与PDO无关,
So basically, your error has nothing specifically to do with PDO, but rather with static vs. member properties.
可以尝试跳过 Sql
类:
class ConnectionManager {
private static $db = false;
private static $database_host = '';
private static $database_user = '';
private static $database_pass = '';
private static $database_db = '';
public static function getConnection() {
if (!self::$db) {
self::connect();
}
return self::$db;
}
private static function connect() {
$dsn = self::database_type . ":dbname=" . self::database_db . ";host=" . self::database_host;
try {
self::$db = new PDO($dsn, self::database_user, self::database_pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
self::$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
//print_r($e->errorInfo);
//echo 'Connection failed: ' . $e->getMessage();
}
}
}
...
class Auth {
public function __construct($db) {
$this->siteKey = 'notImportant';
$this->db = $db;
}
public function foo() {
$stmt = $this->db->prepare('INSERT INTO users (email, password, user_salt, is_admin) VALUES (:email, :password, :user_salt, :is_admin)');
$stmt->execute(...);
}
}
...
$db = ConnectionManager::getConnection();
session_start();
$auth = new Auth($db); // <- Here $db is an actual instance of PDO
这篇关于基于PHP类的用户系统使用PDO - 调用非对象上的成员函数prepare()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!