<?
//数据源
$dsn ='mysql:host=localhost;dbname=sankea';
//用户名
$username='root';
//密码
$password='root';
try{
//开启事务
$pdo->beginTransaction();//执行start transaction;sql语句
//给ID=1 加1000
$sql='UPDATE CS SET amount= amount+1000 WHERE id= 1';
$pdo->exec($sql);//执行sql
//给ID=2 减1000
$sql='UPDATE CS SET amount= amount-1000 WHERE id= 2';
$pdo->exec($sql);//执行sql
//事务提交
$pdo->commit();//执行commit;sql语句
}catch (PDOException $ex){
//
echo'<br>回调';
$pdo->rollback();
echo'<br>错误消息:'.$ex->getMessage();
echo'<br>错误文件:'.$ex->getFile();
echo'<br>错误行号:'.$ex->getLine();
}
try{
//如果try中的代码块,有错误,就去执行catch语句
$pdo= new PDO($dsn,$username,$password);
//PDO报错方式
$pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$sql='select * from cs where id=1';
$stmt=$pdo->query($sql);
}catch(PDOException $e){//相当于$e= new PDOException();
echo'<br>错误消息:'.$e->getMessage();
echo'<br>错误文件:'.$e->getFile();
echo'<br>错误行号:'.$e->getLine();
}