-
-
Save codeforfun-jp/1e1f17cb43cc166c40a16f2816b86001 to your computer and use it in GitHub Desktop.
【PHP】How to use transaction 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* データベース接続のコードは省略 */ | |
try { | |
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | |
echo '---トランザクション開始---<br>'; | |
$pdo->beginTransaction(); | |
$pdo->exec('INSERT INTO user(name, age) VALUES("山田", 20)'); | |
$pdo->exec('INSERT INTO user(name, age) VALUES("鈴木", 25)'); | |
$pdo->exec('INSERT INTO user(name, age) VALUES("佐藤", 30)'); | |
$pdo->commit(); | |
} catch (Exception $e) { | |
$pdo->rollBack(); | |
echo '保存に失敗しました。<br>'; | |
echo 'エラー:'. $e->getMessage() . '<br>'; | |
} finally { | |
echo '---トランザクション終了---'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment