PHP“致命错误:未捕获错误:调用成员函数prepare()为null”
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP“致命错误:未捕获错误:调用成员函数prepare()为null”相关的知识,希望对你有一定的参考价值。
我正在观看YouTube视频,以使用php制作上市网站。但是,当我尝试创建新列表时,这会在我的浏览器中发生:
致命错误:未捕获的错误:调用成员函数prepare()空入D: Dev Dependencies XAMPP htdocs addonlister lib Database.php:31堆栈跟踪:#0D: Dev Dependencies XAMPP htdocs addonlister lib Addon.php(61):数据库->查询('SELECT * FROM a ...')#1D: Dev Dependencies XAMPP htdocs addonlister addon.php(10):抛出Addon-> getAddon('2')#2 {main}D: Dev Dependencies XAMPP htdocs addonlister lib Database.php在线31
这是我在参考页上的代码:
database.php
<?php
class Database{
private $host = DB_HOST;
private $user = DB_USER;
private $pass = DB_PASS;
private $dbname = DB_NAME;
private $dbh;
private $error;
private $stmt;
public function __construct(){
// Set DSN
$dsn = 'mysql:host=localhost'. $this->host .';dbname=addonlister' . $this->dbname;
// Set Options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
);
// PDO Instance
try{
$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
} catch(PDOException $e) {
$this->error = $e->getMessage();
}
}
public function query($query){
$this->stmt = $this->dbh->prepare($query);
}
public function bind($param, $value, $type = null){
if(is_null($type)){
switch(true){
case is_int ( $value ) :
$type = PDO::PARAM_INT;
break;
case is_bool ( $value ) :
$type = PDO::PARAM_BOOL;
break;
case is_null ( $value ) :
$type = PDO::PARAM_NULL;
break;
default :
$type = PDO::PARAM_STR;
}
}
$this->stmt->bindValue($param, $value, $type);
}
public function execute(){
return $this->stmt->execute();
}
public function resultSet(){
$this->execute();
return $this->stmt->fetchAll(PDO::FETCH_OBJ);
}
public function single(){
$this->execute();
return $this->stmt->fetch(PDO::FETCH_OBJ);
}}
Addon.php
<?php
class Addon{
private $db;
public function __construct(){
$this->db = new Database;
}
// Get All Addons
public function getAllAddons(){
$this->db->query("SELECT addons.*, categories.name AS cname
FROM addons
INNER JOIN categories
ON addons.category_id = categories.id
ORDER BY post_date DESC
");
// Assign Result Set
$results = $this->db->resultSet();
return $results;
}
//Get Categories
public function getCategories(){
$this->db->query("SELECT * FROM categories");
// Assign Result Set
$results = $this->db->resultSet();
return $results;
}
// Get Addons By Category
public function getByCategory($category){
$this->db->query("SELECT addons.*, categories.name AS cname
FROM addons
INNER JOIN categories
ON addons.category_id = categories.id
WHERE addons.category_id = $category
ORDER BY post_date DESC
");
// Assign Result Set
$results = $this->db->resultSet();
return $results;
}
// Get category
public function getCategory($category_id){
$this->db->query("SELECT * FROM categories WHERE id = :category_id"
);
$this->db->bind(':category_id' , $category_id);
// Assign Row
$row = $this->db->single();
return $row;
}
// Get Addon
public function getAddon($id){
$this->db->query("SELECT * FROM addons WHERE id = :id");
$this->db->bind(':id' , $id);
// Assign Row
$row = $this->db->single();
return $row;
}
// Addon Job
public function create($data){
//Insert Query
$this->db->query("INSERT INTO addons (category_id, addon_title, description, developer, file_size)
VALUES (:category_id, :addon_title, :description, :developer, :file_size)");
//Bind Data
$this->db->bind(':category_id', $data['category_id']);
$this->db->bind(':addon_title', $data['addon_title']);
$this->db->bind(':description', $data['description']);
$this->db->bind(':developer', $data['developer']);
$this->db->bind(':file_size', $data['file_size']);
//Execute
if($this->db->execute()){
return true;
} else {
return false;
}
}
}
Addon.php
<?php include_once 'config/init.php'; ?>
<?php
$addon = new Addon;
$template = new Template('templates/addon-single.php');
$addon_id = isset($_GET['id']) ? $_GET['id'] : null;
$template->addon = $addon->getAddon($addon_id);
echo $template;
谢谢!
您无需为数据库连接创建函数或类。只需建立一个包含以下内容的连接文件
<?php
try
{
$db = new PDO('mysql:host=localhost; dbname=YourDBname; charset=utf8', 'root', '');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $ex)
{
echo error("Cannot connect to the database!");
die();
}
?>
现在您需要数据库的任何地方都只需要(“ connection.php”);要使用它,您可以通过这种方式使用它
$ExampleQuery= $db->prepare(“SOME QUERY”);
以上是关于PHP“致命错误:未捕获错误:调用成员函数prepare()为null”的主要内容,如果未能解决你的问题,请参考以下文章
IntelliJ IDEA 11编辑php是,支持php文件名为.php5和.php4,如何设置能让其也支持.php呢?