gpt4 book ai didi

php - 从扩展类调用其对象时出现 PDO rowCount() 错误

转载 作者:搜寻专家 更新时间:2023-10-31 21:44:52 25 4
gpt4 key购买 nike

我创建了一个数据库类,它使用 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 中似乎没问题,但所有其他浏览器都会向我抛出我提到的错误...

最佳答案

将您的 row_count-method 更改为此:

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;
}

现在你至少知道出了什么问题。

关于php - 从扩展类调用其对象时出现 PDO rowCount() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5315339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com