我正在尝试在数据库中记录会话。我创建了类,我试图在其构造中调用数据库类。 我收到这个错误:
警告:缺少参数1的SafePDO :: __ construct(),在...中调用
这是我的数据库类:
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/
我整天都在尝试。我的错误在哪里? 谢谢!
答案 0 :(得分:0)
您忘记在此处的会话课程中添加您的dsn:
$this->db = new SafePDO;
由于您的构造没有dsn public function __construct($dsn,
的默认值,因此会抛出错误。这是正常的。
您应该将此dsn提供给Session类,以便能够创建SafePDO
isntance。
我不知道你是如何使用它们的,但你应该运行第一个脚本,然后在创建Session时,执行类似的操作:
$session = new Session($db);
然后在你的Session类中:
class Session
{
private $db;
public function __construct($db)
{
$this->db = $db;
答案 1 :(得分:0)
您构建了SafePDO
类的对象而没有任何参数:
$this->db = new SafePDO;
但$dsn
参数是必需的
你必须设置它。 您应该设置数据库用户名。
见这个例子:
$this->db = new SafePDO('mysql:host=localhost;dbname=example_db;', 'example_usr');