PDO事务管理DEMO

try {

    $dsn = "mysql:host=127.0.0.1;port=3306;dbname=dab"; 

    $pdo = new PDO($dsn, 'root', '');

    $pdo -> setAttribute(PDO::ATTR_AUTOCOMMIT, false);

    $pdo -> beginTransaction();

    $sql = "update DAB_NTOS_ADMIN set name = :name where id = :id";

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

    $sth ->bindValue(':name', 'KILL');

    $sth ->bindValue(':id', 3);

    $flag = $sth-> execute(); // true or false

    echo $flag;

    $pdo -> commit();

} catch (Exception $e) {

    if ($pdo) $pdo -> rollBack();

} finally { 

    $pdo = null;

}

* finally在php5.5支持,老版本不支持

你可能感兴趣的:(demo)