php入門–PDOでトランザクション処理を行うサンプル

phpコード
try {
$dbh = new pdo('mysql:host=localhost;dbname=dbtest’, 'root’, “);
$dbh->query('set names utf8;’);
$dbh->setattribute(pdo::attr_errmode, pdo::errmode_exception);
$dbh->begintransaction();
$dbh->exec(“insert into `sentbl`.`table` (`name` ,`age`)values ('aa’,11);");
$dbh->exec(“insert into `sentbl`.`table` (`name` ,`age`)values ('bb’, 12);");
$dbh->exec(“insert into `sentbl`.`table` (`name` ,`age`)values ('cc’,13);");
$dbh->commit();
} catch (exception $e) {
$dbh->rollback();
echo “insert failed: " . $e->getmessage();
}

PHP

Posted by arkgame