<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> </head> <body> <?php PDO 1.访问不同的数据库 2.自带事务功能 3.防止SQL注入 1.造对象 $dsn = "mysql:dbname=mydb;host=localhost"; $pdo = new PDO($dsn,"root","123"); 2.写SQL语句 $sql = "update nation set name='兽族' where code='n013'"; 3.执行SQL语句 $r = $pdo->query($sql); $r = $pdo->exec($sql);*/ 事务功能 造对象 $dsn = "mysql:dbname=mydb;host=localhost"; $pdo = new PDO($dsn,"root","123"); 设置异常模式 $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); 写SQL语句 $sql1 = "insert into nation values('n016','人族')"; $sql2 = "insert into nation values('n017','不死族')"; 执行两条SQL语句 try { //启动事务 $pdo->beginTransaction(); $pdo->exec($sql1); $pdo->exec($sql2); 提交事务 $pdo->commit(); } catch(PDOException $e) { $e->getMessage(); 回滚 $pdo->rollBack(); } ?> </body> </html>