跨级PDO不执行

时间:2012-11-01 19:49:14

标签: php mysql pdo

我正在重新处理我的所有mysql_ *和mysqli连接以使用PDO,但我似乎无法获得正确的基础知识。我有两个文件:一个创建连接并返回它,一个利用连接用于我认为必要的任何目的。但是出于某种原因,我无法正确地返回连接以供使用。如果我运行任何PDO函数(查询,准备等...),我得到的服务器响应为500,没有错误消息。但我确实知道,如果我在同一个函数中创建连接并查询所有内容,那么所有内容都是hunky-dory。我假设它是简单的东西,所以也许新鲜的眼睛可以帮助我。

db_connect.php(形成连接):

class db_connect {

    function __construct() {}       
    function __destruct() {}

    public function connect() {
        require_once 'db_info.php';
        try{
            $dbh = new PDO('mysql:host='.DB_HOST.';dbname='.DB_DATABASE, DB_USER, DB_PASSWORD);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        } catch (PDOException $e) {
            echo 'Connection Failed: ' . $e->getMessage();
        }
        return $dbh;
    }

    public function close() {
        $dbh = null;
    }
}

operations.php(利用连接):

class operations {

    private $dbh;       

    function __construct() {
        require_once 'db_connect.php';
        $this->dbh = new db_connect();
        $this->dbh->connect();
    }

    function runQuery() {
        // causes 500 error, no logs:
        $stmt = $dbh->query("SELECT * FROM myTable");
    }
}

我确实在寻找一些答案,但我找到的解决方案都没有。提前感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

您实际上是在丢弃您的PDO对象。当你打电话:

$this->dbh = new db_connect();
// $this->dbh is a db_connect

$this->dbh->connect();
// returns a PDO, which you don't save anywhere

然后你会尝试这样做:

$stmt = $dbh->query("SELECT * FROM myTable");
// $dbh doesn't even exist
// You meant $this->dbh, but that won't work either
// $this->dbh is a db_connect object, which does not implement the query() method

我认为您打算将PDO对象保存为db_connect的一部分,然后将其用于查询:

class db_connect {

    public $connection;

    function __construct() {}       
    function __destruct() {}

    public function connect() {
        require_once 'db_info.php';
        try {
            $dbh = new PDO('mysql:host='.DB_HOST.';dbname='.DB_DATABASE, DB_USER, DB_PASSWORD);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        } catch (PDOException $e) {
            echo 'Connection Failed: ' . $e->getMessage();
        }
        $this->connection = $dbh;
        return $dbh;
    }

    public function close() {
        $this->connection = null;
    }
}

class operations {

    private $dbh;       

    function __construct() {
        require_once 'db_connect.php';
        $this->dbh = new db_connect();
        $this->dbh->connect();
    }

    function runQuery() {
        $stmt = $this->dbh->connection->query("SELECT * FROM myTable");
    }
}

最后,如果您只是使用db_connect类来生成PDO连接,那么您应该将connect()转换为静态方法并保存PDO对象(如提及的@dakdad)。在这里,我还将您的require_once移动到更合理的位置:

---------- db_connect.php ----------

require_once "db_info.php";

class db_connect {
    public static function connect() {
        $dbh = null;
        try {
            $dbh = new PDO('pgsql:host='.DB_HOST.';dbname='.DB_DATABASE, DB_USER, DB_PASSWORD);
            $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        } catch (PDOException $e) {
            echo 'Connection Failed: ' . $e->getMessage();
        }
        return $dbh;
    }
}

---------- operations.php ----------

require_once "db_connect.php"

class operations {

    private $dbh;       

    function __construct() {
        $this->dbh = db_connect::connect();
    }

    function runQuery() {
        $stmt = $this->dbh->query("SELECT * FROM words limit 2");
        print_r( $stmt->fetchAll() ) ;
    }
}

答案 1 :(得分:2)

您的operations类应该存储PDO对象,而不是db_connect类。

class operations {

    private $dbh;       

    function __construct() {
        require_once 'db_connect.php';
        $db = new db_connect();
        $this->dbh = $db->connect();
    }

    function runQuery() {
        $stmt = $this->dbh->query("SELECT * FROM myTable");
    }
}

与@slashingweapon的答案类似,只是另一种方式。