【发布时间】:2014-02-28 02:53:25
【问题描述】:
我正在尝试在数据库中记录会话。我创建了类,并试图在其构造中调用数据库类。 我收到此错误:
警告:SafePDO::__construct() 缺少参数 1,调用...
这是我的数据库类:
Class SafePDO extends PDO {
public static function exception_handler($exception) {
// Output the exception details
die('Uncaught exception: ' . $exception->getMessage());
}
public function __construct($dsn, $username = '', $password = '', $driver_options = array()) {
// Temporarily change the PHP exception handler while we...
set_exception_handler(array(__CLASS__, 'exception_handler'));
// ... create a PDO object
parent::__construct($dsn, $username, $password, $driver_options);
// Change the exception handler back to whatever it was before
restore_exception_handler();
}
}
$db = new PDO('mysql:host=localhost;dbname=mynshost_tarrot;charset=utf8', 'mynshost_tarrot', 'PickTarrot2');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
这是我的 Session 课程:
class Session {
private $db;
public function __construct() {
// Instantiate new Database object
$this->db = new SafePDO;
// Set handler to overide SESSION
session_set_save_handler(
array($this, "_open"), array($this, "_close"), array($this, "_read"), array($this, "_write"), array($this, "_destroy"), array($this, "_gc")
);
// Start the session
session_start();
}
/**
* Open
*/
public function _open() {
// If successful
if ($this->db) {
return true;
}
return false;
}
/**
* Close
*/
public function _close() {
// Close the database
if ($this->db->close()) {
return true;
}
return false;
}
/**
* Read
*/
public function _read($id) {
try {
$stmt = $db->prepare("SELECT data FROM sessions WHERE id = :user_id");
$stmt->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
$row_sess_data = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $ex) {
print "<h3>An Error occured!</h3>";
print $ex->getMessage();
}
return $row_sess_data['data'];
}
/**
* Write
*/
public function _write($user_id, $data) {
// Create time stamp
$access = time();
try {
$stmt = $db->prepare("REPLACE INTO sessions VALUES (:user_id, :access, :data)");
$stmt->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$stmt->bindValue(':access', $access, PDO::PARAM_STR);
$stmt->bindValue(':data', $data, PDO::PARAM_STR);
$stmt->execute();
// $row_sess_data = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $ex) {
print "<h3>An Error occured!</h3>";
print $ex->getMessage();
}
}
// End of __write()
/**
* Destroy
*/
public function _destroy($id) {
try {
$stmt = $db->prepare("DELETE FROM sessions WHERE id = :user_id");
$stmt->bindValue(':user_id', $user_id, PDO::PARAM_INT);
$stmt->execute();
} catch (PDOException $ex) {
print "<h3>An Error occured!</h3>";
print $ex->getMessage();
}
}
// End of __destroy()
/**
* Garbage Collection
*/
public function _gc($max) {
// Calculate what is to be deemed old
$old = time() - $max;
try {
$stmt = $db->prepare("DELETE * FROM sessions WHERE access < :old");
$stmt->bindValue(':old', $old, PDO::PARAM_STR);
$stmt->execute();
} catch (PDOException $ex) {
print "<h3>An Error occured!</h3>";
print $ex->getMessage();
}
}
// End of _gc()
}
这两个课程均来自互联网上的真实示例。 SavePDO 来自 PHP.net,Session 类来自这里:http://culttt.com/2013/02/04/how-to-save-php-sessions-to-a-database/
我整天都在尝试。我的错误在哪里? 谢谢!
【问题讨论】: