1

我正在重新处理我的所有 mysql_* 和 mysqli 连接以使用 PDO,但我似乎连基础知识都没有。我有两个文件:一个创建连接并将其返回,另一个将连接用于我认为必要的任何目的。但是,由于某种原因,我无法让连接正确返回以供使用。如果我运行任何 PDO 函数(查询、准备等),我会收到 500 的服务器响应,并且没有错误消息。但是,我确实知道,如果我在同一个函数中创建连接并查询所有内容,那么一切都是笨拙的。我假设这很简单,所以也许新鲜的眼睛可以帮助我。

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

我确实四处寻找一些答案,但我找到的解决方案都不适合我。提前感谢您的帮助。

4

2 回答 2

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() ) ;
    }
}
于 2012-11-01T20:00:13.537 回答
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 的答案类似,只是另一种方式。

于 2012-11-01T20:39:32.187 回答