[英]PHP PDO class programming:Fatal error: Call to a member function fetchAll() on boolean
[英][PHP / PDO CLASS]Fatal error: Call to a member function fetch() on boolean
每當我嘗試使用 PDO 數據庫包裝器從我的數據庫中獲取某些內容時,我都會收到此錯誤:
Fatal error: Call to a member function fetch() on boolean in C:\\xampp\\htdocs\\index.php on line 17
索引.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;
類.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() 函數?
你做錯了很多事情,(完全改變了方法名稱! )但是你得到的錯誤信息很清楚:如果你想使用方法鏈,你應該像 prepare iswritten() 一樣編寫每個方法 - 讓它返回該聲明。
但是,這個類無法挽救,因為它的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();
它不像您目前的方法那么整潔,但同樣 - 它沒有其可怕的缺點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.