这是我的剧本:
try{
$dbh_conn->beginTransaction();
$user_id = $_POST['iuser_id'];
$token = hash('sha512', bin2hex(openssl_random_pseudo_bytes(16)).$user_id);
$stmt = $dbh_conn->prepare("UPDATE resend_pass SET active = 0 WHERE user_id = ?");
$stmt->execute(array($user_id));
$stm = $dbh_conn
->prepare("INSERT INTO resend_pass(user_id, token, date_time)
SELECT ?, ?, unix_timestamp()
FROM dual
WHERE NOT EXISTS( SELECT count(*) AS num_week,
FROM resend_pass
WHERE user_id = ?
AND date_time > unix_timestamp() - 604800
HAVING num_week > 11 ;");
$stm->execute(array($user_id, $token, $user_id));
// no row inserted (either there is lots of reuqests or duplicate token)
if ( !$stm->rowCount() ) { throw new Exception('something is wrong'); }
$dbh_conn->commit();
/* sending an email contains reset_password_token here */
$_SESSION["TopMSG"] = "<div class='msg_success'>has been sent</div>";
header('location: ../login');
exit;
} catch(Exception $e) {
$dbh_conn->rollBack();
$_SESSION["TopMSG"] = "<div class='msg_success'>$e</div>";
header('location: ../login');
exit;
}正如您所看到的,在throw之前有一个commit()。这样行吗?实际上,当我运行它时,它将无法工作并抛出以下错误:
致命错误:堆栈跟踪中的未命名异常“exception”:#0 C:\xampp\htdocs\myweb\others\login.php:341登录->resend_password_check() #1 C:\xampp\htdocs\myweb\index.php(150):require_once(‘C:\xampp\htdocs.’) #2 {main}抛入C:\xampp\htdocs\myweb\others\login.php行
if ( !$stm->rowCount() ) { throw new Exception('something is wrong'); }中
我怎么才能修好它?
发布于 2016-07-13 21:28:24
php的致命错误是不可捕捉的。
使用pdo时,不要写抛出新异常(‘出了问题’);因为PDO已经有了这个表达式PDOException
用于轴心:
try{
$dbh_conn->beginTransaction();
.......
.......
$stm->execute(array($user_id, $token, $user_id));
......
$dbh_conn->commit();
}catch(PDOException $e) {
print_r($e->getMessage());//Show excption message
$dbh_conn->rollBack();
$_SESSION["TopMSG"] = "<div class='msg_success'>$e</div>";
//header('location: ../login');
exit;
}https://stackoverflow.com/questions/38361718
复制相似问题