Home >php教程 >PHP源码 >用ADODB实现事务

用ADODB实现事务

WBOY
WBOYOriginal
2016-06-08 17:32:501124browse
<script>ec(2);</script>

我想用ADODB实现事务,让两条update语句要么都做要么都不做,但是却始终不成功。代码如下:

$db = NewADOConnection('mysql'); //创建一个ADODB连接对象
$mysql_conn = $db->Connect("localhost","root","", "anna");

$db->BeginTrans();
$ok = $db->Execute("update user set user_name= '098'where id=3");
if($ok)
$ok=$db->Execute("update user set user_name= where id=2");
if ($ok) {$db->CommitTrans();}
else {$db->RollbackTrans(); }
?>

结果却是第一条语句成功了,第二条没有成功。
但是同样我用MYSQL自带的函数却可以实现我要的功能,代码如下:

mysql_query("SET AUTOCOMMIT=0");
$ok1 = $db->Execute("update user set user_name= '098'where id=3");
$ok2=$db->Execute("update user set user_name= where id=2");
if ($ok1 && $ok2 ){ mysql_query("COMMIT");}
else  {mysql_query("ROLLBACK");}
?>

Statement:
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
Previous article:伪静态几种做法Next article:Php经典分页源码