Tôi đang gặp sự cố với PDO.
Mỗi khi tôi chạy tệp php. Tôi nhận được lỗi sau đây :
Lỗi nghiêm trọng : Uncaught Error: Gọi hàm thành viên chuẩn bị() trên null…
Tệp php:
try {
//connection
include '../connection.php';
$database = new Database;
$database->query('SELECT MAX(id) AS max_id FROM subscriber');
$rows = $database->resultset();
$id = $rows[0]["max_id"] + 1; // add with new ID
....
Kết nối.php
<?php
//defining the connection and other function that will be perform for linking with database
class Database
{
private $host="";
private $user="";
private $pass="";
private $dbname="data.db";
private $dbh; //database handling
private $error;
private $stmt; //statements
public function __construct()
{
// Set DSN(Data Source Name)
//SQLITE
$dsn = 'sqlite:'.$this->dbname;
// Set Options
$options = array(
PDO::ATTR_PERSISTENT => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => true
);
// Create new PDO
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);
}
}
//to execute prepared statements
public function execute()
{
return $this->stmt->execute();
}
//to select records from database
public function resultset()
{
$this->execute();
// retrieve the resultset in the form of an associative array
return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
}
//execute function lastInserted Id
public function lastInsertId()
{
$this->dbh->lastInsertId();
}
}
tôi thử một số giải pháp trên google nhưng vẫn gặp lỗi. Tôi có bỏ lỡ điều gì không?
Cảm ơn trước,
vó ngựa
Chủ đề này đã tự động đóng sau 182 ngày kể từ lần trả lời cuối cùng. Trả lời mới không còn được phép.