求教关于PDO使用的问题
相关的数据库是这样的:
<br />CREATE TABLE `session` (<br /> `skey` char(32) CHARACTER SET ascii NOT NULL,<br /> `data` text COLLATE utf8mb4_bin,<br /> `expire` int(11) NOT NULL,<br /> PRIMARY KEY (`skey`)<br />) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;<br />
<br />define('DNS', 'mysql:host=localhost;dbname=db;charset=utf8mb4');<br />define('USR', 'usr');<br />define('PWD', 'pwd');<br />define('MAXLIFETIME', 1440);<br /><br />function write($id, $data) {<br /> try {<br /> $dbh = new PDO(DNS, USR, PWD, array(<br /> PDO::ATTR_PERSISTENT => TRUE,<br /> PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,<br /> PDO::ATTR_EMULATE_PREPARES => FALSE<br /> ));<br /> try {<br /> $expire = time() + MAXLIFETIME;<br /> $sql = 'INSERT INTO `session` (`skey`, `data`, `expire`) '<br /> . 'VALUES (:skey, :data, :expire) '<br /> . 'ON DUPLICATE KEY UPDATE '<br /> . '`data` = :data, `expire` = :expire';<br /> $stmt = $dbh->prepare($sql);<br /> $stmt->bindValue(':skey', $id, PDO::PARAM_STR);<br /> $stmt->bindValue(':data', $data, PDO::PARAM_STR);<br /> $stmt->bindValue(':expire', $expire, PDO::PARAM_INT);<br /> $stmt->execute();<br /> $dbh = NULL;<br /> } catch (Exception $e) {<br /> echo $e->getTraceAsString();<br /> }<br /> } catch (Exception $e) {<br /> echo $e->getTraceAsString();<br /> }<br />}<br /><br />write('12345678', '87654321');<br />
<br />PDO::ATTR_PERSISTENT => TRUE,<br />PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,<br />