PDO:调用未定义的方法DB :: query()

时间:2011-06-10 14:01:48

标签: php mysql pdo

试图摆脱PDO,但目前没有太多乐趣。我打赌它也很简单。

编辑:这样做更好的方法是什么? ,而不是把它包装在一个类?

类/ 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();
        }

    }

}

?>

包括/ global.inc.php:

<?php

require_once 'classes/DB.class.php';

// Establish Connection to Database.

$db = new DB();
$db->connect();

?>

的index.php:

<?php

require_once 'includes/global.inc.php';

$STH = $db->query("SELECT * FROM users");
echo "<pre>";
print_r($STH->fetch());

?>

4 个答案:

答案 0 :(得分:3)

您的数据库类中没有DB属性和query()方法。像这个列表一样添加它:

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

更好的方法是使用一些ORM库或裸PDO对象 - 它非常友好。

答案 1 :(得分:2)

试试这个:

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');

此外,我在您的成员面前添加了$this关键字,因为$db_name等未在方法范围内声明。

如果您不希望在创建对象时启动连接,则可以执行以下操作:

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');

重要说明:通常,在覆盖子项中的方法时,您需要指定与父项相同的方法签名(否则您将收到E_STRICT错误)。幸运的是,这不适用于核心类,可能允许这样的覆盖。

答案 2 :(得分:1)

实例化为$ db的对象没有方法查询。可能不需要将PDO对象包装在另一个对象中,但如果要这样做,则需要确保所有方法都可以访问。

答案 3 :(得分:1)

你的班级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');
            }
         }
     }

}

?>

使用魔术函数__call(),您可以将PDO支持的所有函数传递给新创建的PDO对象。