Home > Database > Mysql Tutorial > 基于mysql事务、视图、存储过程、触发器的应用分析_MySQL

基于mysql事务、视图、存储过程、触发器的应用分析_MySQL

WBOY
Release: 2016-06-01 13:24:19
Original
993 people have browsed it

bitsCN.com

一 ,mysql事务

MYSQL中只有INNODB类型的数据表才能支持事务处理。

启动事务有两种方法

(1) 用begin,rollback,commit来实现

begin  开始一个事务
rollback   事务回滚
commit    事务确认

(2)直接用set来改变mysql的自动提交模式

set autocommit=0 禁止自动提交
set autocommit=1 开启自动提交

demo

header("Content-type:text/html;charset=utf-8");
mysql_pconnect("localhost","root","") or die("数据库连接失败");
mysql_select_db("test");
mysql_query("set names utf8");
//开启一个事务
//mysql_query("BEGIN");
//mysql_query("START TRANSACTION");
//mysql_query("SET AUTOCOMMIT=1");//设置事务不自动提交 mysql默认是自动提交
mysql_query("SET AUTOCOMMIT=1");//开启事务
$sql1 = "INSERT INTO `test`values ('2222','测试数据')";
$sql2 = "INSERT INTO `test` values ('111','sss','22')";//特地写的错误
$res1 = mysql_query($sql1);
$res2 = mysql_query($sql2);
if($res1 && $res2)
{
    mysql_query("COMMIT");
    echo "事务提交";
}else{
    mysql_query("ROLLBACK");
    echo "事务回滚";
}
mysql_query("END");

bitsCN.com
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template