时间:2021-07-01 10:21:17 帮助过:3人阅读
CREATE TABLE `session` (
`skey` char(32) CHARACTER SET ascii NOT NULL,
`data` text COLLATE utf8mb4_bin,
`expire` int(11) NOT NULL,
PRIMARY KEY (`skey`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;
define('DNS', 'mysql:host=localhost;dbname=db;charset=utf8mb4');
define('USR', 'usr');
define('PWD', 'pwd');
define('MAXLIFETIME', 1440);
function write($id, $data) {
try {
$dbh = new PDO(DNS, USR, PWD, array(
PDO::ATTR_PERSISTENT => TRUE,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => FALSE
));
try {
$expire = time() + MAXLIFETIME;
$sql = 'INSERT INTO `session` (`skey`, `data`, `expire`) '
. 'VALUES (:skey, :data, :expire) '
. 'ON DUPLICATE KEY UPDATE '
. '`data` = :data, `expire` = :expire';
$stmt = $dbh->prepare($sql);
$stmt->bindValue(':skey', $id, PDO::PARAM_STR);
$stmt->bindValue(':data', $data, PDO::PARAM_STR);
$stmt->bindValue(':expire', $expire, PDO::PARAM_INT);
$stmt->execute();
$dbh = NULL;
} catch (Exception $e) {
echo $e->getTraceAsString();
}
} catch (Exception $e) {
echo $e->getTraceAsString();
}
}
write('12345678', '87654321');
PDO::ATTR_PERSISTENT => TRUE,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,