每当我尝试使用我的PDO数据库包装器从我的数据库中获取内容时,我都会收到此错误:
Fatal error: Call to a member function fetch() on boolean in C:\xampp\htdocs\index.php on line 17
的的index.php
require_once 'class.db.php';
$mysql = [
'hostname' => '127.0.0.1',
'database' => 'lucidcms',
'username' => 'root',
'password' => 'test',
'charset' => 'utf8',
];
$db = new Database($mysql);
$u = "Sentinel";
$result = $db->prepare("SELECT `id` FROM `users` WHERE `username` = :u LIMIT 1")
->execute([':u' => $u])
->fetch();
echo $result;
class.db.php
<?php
class Database {
private static $stmt, $pdo;
public function __construct(array $mysql) {
if(is_null(self::$pdo)) {
try {
self::$pdo = new \PDO(
'mysql:dbname='.$mysql['database'].';host='.$mysql['hostname'].';charset='.$mysql['charset'],
$mysql['username'],
$mysql['password'],
[\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_PERSISTENT => true]
);
} catch(PDOException $e) {
die('An error occurred when trying to communicate with the database.');
//die($e->getMessage());
}
} else {
return new self;
}
}
public function __destruct() {
// Destruct the connection to DB once finished
try {
self::$pdo = null;
} catch(PDOException $e) {
die('An error occurred when trying to close the connection with the database.');
//die($e->getMessage());
}
}
public static function prepare($sql) {
self::$stmt = self::$pdo->prepare($sql);
return self::$stmt;
}
public static function execute($data = []) {
return self::$stmt->execute(isset($data) ? $data : null);
}
public static function count() {
return self::$stmt->rowCount();
}
public static function fetch() {
return self::$stmt->fetchColumn();
}
public static function fetchAll($type = \PDO::FETCH_ASSOC) {
return self::$stmt->fetch($type);
}
public static function lastId() {
return self::$pdo->lastInsertId();
}
}
我似乎无法找到解决此错误的方法 我做错了什么想法?
编辑,新的工作解决方案,但编码非常混乱:<?php
class DB {
private static $host;
private static $db;
private static $dbuser;
private static $pass;
private static $char;
private static $pdo, $stmt;
public function data($mysql) {
self::$host = $mysql['hostname'];
self::$db = $mysql['database'];
self::$dbuser = $mysql['username'];
self::$pass = $mysql['password'];
self::$char = $mysql['charset'];
}
protected function __clone() {}
public static function connect() {
if(is_null(self::$pdo)) {
try {
self::$pdo = new \PDO(sprintf("mysql:host=%s;dbname=%s;",
self::$host,
self::$db),
self::$dbuser,
self::$pass,
[
\PDO::ATTR_EMULATE_PREPARES => false,
\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION
]
);
} catch(PDOException $e) {
die('An error occurred when trying to communicate with the database.');
}
} else {
return self::$pdo;
}
}
public function __destruct() {
// Destruct the connection to DB once finished
try {
self::$pdo = null;
} catch(PDOException $e) {
die('An error occurred when trying to close the connection with the database.');
//die($e->getMessage());
}
}
public static function prepare($sql) {
DB::connect();
self::$stmt = self::$pdo->prepare($sql);
return new self;
}
public static function execute($data = []) {
DB::connect();
try {
self::$stmt->execute(isset($data) ? $data : null);
} catch(PDOException $e) {
die("Execution error: " . $e->getMessage());
}
return new self;
}
public static function fetchAll($type = \PDO::FETCH_BOTH) {
DB::connect();
return self::$stmt->fetch($type);
}
public static function fetch() {
DB::connect();
return self::$stmt->fetchColumn();
}
}
是否可以将connect()和data()转换为__construct()函数?
答案 0 :(得分:0)
你做错了很多事情,(单独完全改变了方法名称!)但是你得到的错误信息是明确的:如果你想使用方法链接,你应该像编写每个方法一样准备写() - 使其返回声明。
但是,这个类无法挽救,因为它会statefulness导致生产中出现令人讨厌的错误。让我建议另一个解决方案,更轻量级,更容易出错,而不是这个麻烦的混乱
class MyPDO extends PDO
{
public function run($sql, $args = NULL)
{
$stmt = $this->prepare($sql);
$stmt->execute($args);
return $stmt;
}
}
通过这个简单的扩展,您将能够在SELECT查询中使用方法链接,而不会有乱码的风险。
$result = $db->run("SELECT `id` FROM `users` WHERE `username` = :u LIMIT 1", [':u' => $u])
->fetch();
它不像你现在的方法那样整洁但又不是 - 它没有它的可怕缺点。