有事务时抛出异常
Throwing an exception when there is transaction
这是我的脚本:
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;
}
如您所见,commit()
之前有一个 throw
。这样好吗?实际上当我 运行 它时,它不会工作并抛出这个错误:
Fatal error: Uncaught exception 'Exception' in C:\xampp\htdocs\myweb\others\login.php:341 Stack trace: #0 C:\xampp\htdocs\myweb\application\other.php(35): login->resend_password_check() #1 C:\xampp\htdocs\myweb\index.php(150): require_once('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\myweb\others\login.php on line if ( !$stm->rowCount() ) { throw new Exception('something is wrong'); }
我该如何解决?
php 无法捕获致命错误。
使用pdo时不要写throw new Exception('something is wrong');
因为 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;
}
这是我的脚本:
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;
}
如您所见,commit()
之前有一个 throw
。这样好吗?实际上当我 运行 它时,它不会工作并抛出这个错误:
Fatal error: Uncaught exception 'Exception' in C:\xampp\htdocs\myweb\others\login.php:341 Stack trace: #0 C:\xampp\htdocs\myweb\application\other.php(35): login->resend_password_check() #1 C:\xampp\htdocs\myweb\index.php(150): require_once('C:\xampp\htdocs...') #2 {main} thrown in C:\xampp\htdocs\myweb\others\login.php on line
if ( !$stm->rowCount() ) { throw new Exception('something is wrong'); }
我该如何解决?
php 无法捕获致命错误。
使用pdo时不要写throw new Exception('something is wrong'); 因为 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;
}