【发布时间】:2011-03-15 17:10:44
【问题描述】:
我有一个使用 PDO 连接到我的查询的数据库类,我在我的主类构造函数中将它作为对象调用。
class MyClass
{
protected $db;
public __constructor()
{
$this->db = new Database();
}
}
class Themes extends MyClass
{
public $avatar;
public $theme_name;
public $theme_by;
public $theme_by_email;
public $theme_by_website;
public $theme_description;
public $theme_thumb;
public $theme_source;
public $theme_css;
public $theme_js;
public $theme_uploaded_on;
public function __construct()
{
parent::__construct();
$this->get_theme();
$this->get_avatar();
}
public function get_theme()
{
$sql = "SELECT *
FROM `user_themes`
WHERE `user_id` = " . $this->session->get('user_id');
if($this->db->row_count($sql))
{
$result = $this->db->fetch_row_assoc($sql);
$this->theme_name = $result['theme_name'];
$this->theme_by = $result['theme_by'];
$this->theme_by_email = $result['theme_by_email'];
$this->theme_by_website = $result['theme_by_website'];
$this->theme_description = $result['theme_description'];
$this->theme_source = $result['theme_source'];
$this->theme_css = $result['theme_css'];
$this->theme_js = $result['theme_js'];
$this->theme_uploaded_on = $result['theme_uploaded_on'];
}else{
die('no results');
}
}
}
我的问题是,如果我包含调用 MyClass 构造函数的扩展类,则会收到此错误:
Fatal error: Call to a member function rowCount() on a non-object in db.class.php on line 98
在我的 db.class.php 中指向这一行
class Database {
private static $PDO;
private static $config;
public function __construct() {
if (!extension_loaded('pdo'))
die('The PDO extension is required.');
self::$config = config_load('database');
self::connect();
}
...
public function row_count($statement)
{
return self::$PDO->query($statement)->rowCount(); //Line 98
}
}
如果我从我的扩展类中注释掉 parent::__construct() 那么我没问题并且没有错误。
在 FireFox、Chrom、Opera 和 Safari 中试用我的网站
我在 Firefox 3.6 中似乎还可以,但所有其他浏览器都会向我抛出我提到的错误...
【问题讨论】:
-
我收到错误消息... (Firefox 4.0)
-
你是否调用了两次该方法?根据 php.net,这可能会失败:be2.php.net/manual/en/pdo.query.php