php PDO:调用未定义的方法 DB::query()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6307408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PDO: Call to undefined method DB::query()
提问by ritch
Trying to get to hang of PDO but not having much fun at the moment. I bet it's something really simple as well.
试图掌握 PDO,但目前没有太多乐趣。我敢打赌,这也很简单。
EDIT: What would be a better way to go about doing this? i.e instead of wrapping it in a class?
编辑:这样做的更好方法是什么?即而不是将它包装在一个类中?
classes/DB.class.php:
类/DB.class.php:
<?php
// DB.class.php
class DB {
protected $db_name = "PDO";
protected $db_user = "root";
protected $db_pass = "root";
protected $db_host = "localhost";
// Establish Connection to Database.
public function connect() {
try {
$DB = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass);
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
}
?>
includes/global.inc.php:
包括/global.inc.php:
<?php
require_once 'classes/DB.class.php';
// Establish Connection to Database.
$db = new DB();
$db->connect();
?>
index.php:
索引.php:
<?php
require_once 'includes/global.inc.php';
$STH = $db->query("SELECT * FROM users");
echo "<pre>";
print_r($STH->fetch());
?>
采纳答案by Tomasz Kowalczyk
You don't have DB property and query()
method in your DB class. Add it like this listing:
您query()
的 DB 类中没有 DB 属性和方法。像这样添加它:
class DB
{
protected $db_name = "PDO";
protected $db_user = "root";
protected $db_pass = "root";
protected $db_host = "localhost";
protected $DB = null;
// Establish Connection to Database.
public function connect()
{
try
{
$this->DB = new PDO("mysql:host=".$this->db_host.";dbname=".$this->db_name."", $this->db_user, $this->db_pass);
}
catch(PDOException $e)
{
echo $e->getMessage();
}
}
public function query()
{
return $this->DB->query();
}
}
The better way would be to use some ORM library or bare PDO object - it's quite friendly.
更好的方法是使用一些 ORM 库或裸 PDO 对象 - 它非常友好。
回答by netcoder
Try this:
尝试这个:
class DB extends PDO {
protected $db_name = "PDO";
protected $db_user = "root";
protected $db_pass = "root";
protected $db_host = "localhost";
public function __construct() {
try {
parent::__construct("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
$db = new DB;
$db->query('SELECT * FROM something');
Also, I added the $this
keyword in front of your members, because $db_name
and such were not declared in the method scope.
另外,我$this
在您的成员前面添加了关键字,因为$db_name
没有在方法范围内声明。
If you do not want the connection to be initiated when the object is created, you could do:
如果您不希望在创建对象时启动连接,您可以执行以下操作:
class DB extends PDO {
protected $db_name = "PDO";
protected $db_user = "root";
protected $db_pass = "root";
protected $db_host = "localhost";
public function __construct() {
// do nothing
}
public function connect() {
try {
parent::__construct("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass);
} catch (PDOException $e) {
echo $e->getMessage();
}
}
}
$db = new DB;
$db->connect();
$db->query('SELECT * FROM something');
Important note:Normally, when overriding methods in children, you need to specify the same method signature as the parent (or you'll get an E_STRICT error). Fortunately, this doesn't apply to core classes, probably to allow overrides like this.
重要说明:通常,当覆盖子级中的方法时,您需要指定与父级相同的方法签名(否则会出现 E_STRICT 错误)。幸运的是,这不适用于核心类,可能允许像这样覆盖。
回答by datasage
The object instantiated as $db does not have the method query. There is probably not a need to wrap the PDO object within another object, but if you are going to do that, you need to make sure all methods are accessible.
实例化为 $db 的对象没有方法查询。可能不需要将 PDO 对象包装在另一个对象中,但是如果您打算这样做,您需要确保所有方法都可以访问。
回答by Sascha Galley
your class DB has no method query! you could do it this way:
您的类 DB 没有方法查询!你可以这样做:
<?php
// DB.class.php
class DB {
protected $db_name = "PDO";
protected $db_user = "root";
protected $db_pass = "root";
protected $db_host = "localhost";
private $_db;
// Establish Connection to Database.
public function connect() {
try {
$this->_db = new PDO("mysql:host=".$this->db_host.";dbname=".$this->db_name, $this->db_user, $this->db_pass);
}
catch (PDOException $e) {
echo $e->getMessage();
}
}
public function __call($name, array $arguments) {
if(method_exists($this->_db, $name)){
try{
return call_user_func_array(array(&$this->_db, $name), $arguments);
}
catch(Exception $e){
throw new Exception('Database Error: "'.$name.'" does not exists');
}
}
}
}
?>
With the magic function __call()
you can pass all functions that PDO supports to your newly created PDO Object.
使用魔法函数,__call()
您可以将 PDO 支持的所有函数传递给您新创建的 PDO 对象。