【问题标题】:PDO rowCount() error when calling its object from extended classes从扩展类调用其对象时 PDO rowCount() 错误
【发布时间】: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 中试用我的网站

http://www.helixagent.com

我在 Firefox 3.6 中似乎还可以,但所有其他浏览器都会向我抛出我提到的错误...

【问题讨论】:

标签: php pdo


【解决方案1】:

将您的 row_count 方法更改为:

public function row_count($statement)
{
  self::$PDO->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  try {
    $stmt = self::$PDO->query($statement); 
    $result = $stmt->rowCount();
    return $result;
  } catch (PDOException $e) { echo $e->getMessage(); }

    return false;
}

现在你至少会知道哪里出了问题。

【讨论】:

    【解决方案2】:

    您的构造函数应该被称为__construct(),而不是__constructor()。所以你的代码在调用父构造函数时可能会失败。

    此外,当 query() 方法失败时,返回 false 而不是您可以在其上调用 rowcount() 的对象。您应该添加一个检查以查看查询是否成功。

    【讨论】:

    • hmmmm,您在查询时指的是我的子类还是我的 db.class()->?
    • 我指的是 MyClass 的构造函数部分。对于 query() 方法,我指的是您的 row_count($statement) 方法。
    【解决方案3】:

    此摘录来自此处 (http://php.net/manual/en/language.oop5.basic.php)

    $this 和 self 有什么区别?

    在类定义中,$this 指向当前对象,而 self 指向当前类。

    需要使用 self 引用类元素,使用 $this 引用对象元素。

    因此在你的函数 row_count(...) 你应该使用 $this->PDO->query(...

    [编辑]

    你可以尝试声明 $this->db = null;在你调用你的父类构造之前,在你的子类中。

    class MyOtherClassB extends MyClass
    {
        public __construct()
        {
            $this->db = null;
            parent::__construct();
        }
    

    【讨论】:

    • 我使用 self:: 因为我有一个 $PDO 的私有静态变量,我将引用传递给
    • 你能给我举个例子,你想让我把 null 放在哪里吗?
    • 你在 db.class.php 中是否声明了 $PDO?
    • 不,似乎没有清空变量。太奇怪了,是不是因为我在包含我的子类时调用了数据库对象 2 次?
    • 我认为问题与您如何启动 $PDO 有关。尝试单独 return self::$PDO->query($statement)->rowCount() 到 $result = self::$PDO->query($statement);返回 $result->rowCount();
    猜你喜欢
    • 2011-09-12
    • 2016-01-03
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多