具体如下:
$servername="localhost";
$username="root";
$password="admin";
$dbname="test";
try{
$conn=new PDO("mysql:host=$servername;dbname=$dbname",$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
//开始事务
$conn->beginTransaction();
$conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('YE','XIAMING','1111@163.com')");
$conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('YE','CONG','2222@163.com')");
$conn->exec("INSERT INTO `hello`(`firstname`,`lastname`,`email`)VALUES('FANG','MENG','3333@163.com')");
//提交事务
$conn->commit();
echo "New records created successfully!";
}catch(PDOException $e){
//回滚事务
$conn->rollBack();
echo $sql."<br>".$e->getMessage();
}
$conn=NULL;| 欢迎光临 一起源码网 (https://www.171739.xyz/) | Powered by Discuz! X3.3 |