Php 简单PDO包装器不工作
我正在尝试这个包装器,不管怎样,当我在index.php文件中var_dump($row)时,我总是得到“boolean false” 这是我的db类:Php 简单PDO包装器不工作,php,pdo,wrapper,Php,Pdo,Wrapper,我正在尝试这个包装器,不管怎样,当我在index.php文件中var_dump($row)时,我总是得到“boolean false” 这是我的db类: <?php // database.php class DB { protected $_connection = array(); // to map the connection protected $_dbh; // property which keeps the database handl
<?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();
}
}
但是var\u dump($r)总是返回布尔值false,有时当我尝试使用代码执行不同操作时,会得到“调用未定义的方法DB::single()”,或“非对象上的方法”类消息
有人能帮我解决这个问题吗
非常感谢。问候。好的,现在它工作得很好。代码如下:
<?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();
}
}
您似乎没有选择数据库,因此可能是您的查询出错了。添加数组(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)
作为new PDO()
构造函数的最后一个参数,以便更好地调试。为了我的理智起见,如果没有其他内容,请大家停止为PDO编写包装器类。它不需要OO包装,它已经是OO了。如果你想要额外的功能/sugar方法,扩展它。尝试用Try/catch包装execute并检查错误代码@kevinmajor1@kevinmajor1,放松点,伙计们。我在学这些东西。没必要粗鲁。建议一个更好的解决方案与提出一个更有经验的程序员比提出问题而不是侮辱的程序员有区别。如果你讨厌帮助新手,就不要帮助他们。
<?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
<?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();
}
}
<?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);