close

例1 提交一個基礎事務

<?php
/* 開始一個事務,關閉自動提交 */
$dbh->beginTransaction();

/* 在全有或全無的基礎上插入多行記錄(要麽全部插入,要麽全部不插入) */
$sql = 'INSERT INTO fruit
(name, colour, calories)
VALUES (?, ?, ?)';

$sth = $dbh->prepare($sql);

foreach ($fruits as $fruit) {
$sth->execute(array(
$fruit->name,
$fruit->colour,
$fruit->calories,
));
}

/* 提交更改 */
$dbh->commit();

/* 現在數據庫連接返回到自動提交模式 */
?>

 

例2 提交一個DDL事務

<?php
/* 開始一個事務,關閉自動提交 */
$dbh->beginTransaction();

/* Change the database schema */
$sth = $dbh->exec("DROP TABLE fruit");

/* 更改數據庫架構 */
$dbh->commit();

/* 現在數據庫連接返回到自動提交模式 */
?>

arrow
arrow
    全站熱搜

    stockwfj3 發表在 痞客邦 留言(0) 人氣()