简单的PDO包装器无法正常工作 [英] Simple PDO Wrapper not working

查看:90
本文介绍了简单的PDO包装器无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试用此包装器,无论我在index.php文件中var_dump($ row)时总是得到布尔假".

I'm trying out this Wrapper and no matter what I always just get 'boolean false' when I var_dump($row) in my index.php file.

这是我的数据库类:

<?php
// database.php
class DB {
protected $_connection = array();  // to map the connection
protected $_dbh;                   // property which keeps the database handler
protected $_database;              // property to keep the database name
protected $_table;                 // property to keep the table name
protected $_query;                 // property to keep a query statement

public function __construct() {

    // require the database configurations as an array from config.php
    $this->_connection = require_once 'config.php';
    // define which database driver to use (mysql?) and extract the values as 
    // variables
    extract($this->_connection['connection']['mysql']);

    // make the database and table name available to the class
    $this->_database = $database;
    $this->_table    = $table_name;

    // Check for PDO driver and create the connection to the database.
    try {
        $this->_dbh = new PDO($driver.":host=".$host, $username, $password);
    } catch (PDOException $e) {
        die ('Connection failed: ' . $e->getMessage());
    }
}

public function input_query($query) {
    $this->_query = $this->_dbh->prepare($query);
    return $this;
}

public function execute() {
    return $this->_query->execute();
}

public function single() {
    $this->execute();
    return $this->_query->fetch();
}

public function result_set() {
    $this->execute();
    return $this->fetchAll();
}    
}

然后在我的index.php文件中:

And then in my index.php file:

<?php
// index.php
require_once 'database.php';

$db = new DB();

$db->input_query("SELECT `name` FROM `names` WHERE `id`=1");
$r = $db->single();

var_dump($r);

如果我愿意

<?php
// index.php 
require_once 'database.php';

$db = new DB();
var_dump($db);

我得到一个对象:

object(DB)[1]
  public '_connection' => 
    array (size=1)
      'connection' => 
        array (size=4)
          'sqlite' => 
            array (size=3)
              ...
          'mysql' => 
            array (size=6)
              ...
          'pgsql' => 
            array (size=6)
              ...
          'sqlsrv' => 
            array (size=6)
              ...
  protected '_dbh' => 
    object(PDO)[2]
  protected '_database' => string 'pagination_demo' (length=15)
  protected '_table' => string 'names' (length=5)
  protected '_query' => null

但是 var_dump($ r)总是返回布尔值false,有时当我用代码尝试其他操作时,我会得到'调用未定义的方法DB :: single()'关于非对象的方法" 这类消息.

But var_dump($r) always returns boolean false and sometimes when I try different things with the code I get 'Call to an undefined method DB::single()', or 'method on a non-object' kind of message.

有人可以帮我解决这个问题吗?

Can anyone help me sort this out?

非常感谢.问候.

推荐答案

好,现在可以正常使用了.这是代码:

Ok, now it works perfectly. And here is the code:

<?php // database.php
class DB extends PDO {

    protected $_connection = array();         // to map the connection

    protected $_dbh;                        // property which keeps the database handler

    protected $_database;                   // property to keep the database name

    protected $_table;                      // property to keep the table name

    protected $_query;                       // property to keep a query statement

    public function __construct() {

        // require the database configurations as an array from config.php
        $this->_connection = require_once 'config.php';
        // define which database driver to use (mysql?) and extract the values as variables
        extract($this->_connection['connection']['mysql']);

        // make the database and table name available to the class
        $this->_database = $database;
        $this->_table    = $table_name;

        // build the dsn string value for the PDO connection
        $dsn = $driver.":host=".$host.";dbname=".$database;

        // Check for PDO driver and create the connection to the database.
        try {
            parent::__construct($dsn, $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
        } catch (PDOException $e) {
            die ('Connection failed: ' . $e->getMessage());
        }
    }

    public function input_query($query) {
        $this->_query = parent::prepare($query);
        return $this;
    }

    public function execute() {
        return $this->_query->execute();
    }

    public function single() {
        $this->execute();
        return $this->_query->fetch();
    }

    public function result_set() {
        $this->execute();
        return $this->_query->fetchAll();
    }
}

还有index.php文件:

And the index.php file:

<?php
require_once 'database.php';

$db = new DB();

$db->input_query("SELECT `name` FROM `names`");

try {
    $r = $db->result_set();
} catch (PDOException $e) {
    die ($e->getMessage());
}
var_dump($r);

然后var_dump($ r)给了我一大堆名字.

And var_dump($r) gave me a whole bunch of names.

非常感谢你们的帮助!

这篇关于简单的PDO包装器无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆