Home  >  Article  >  Backend Development  >  How to implement ADODB transaction processing in PHP

How to implement ADODB transaction processing in PHP

php中世界最好的语言
php中世界最好的语言Original
2018-05-28 09:59:081585browse

This time I will show you how to implement ADODB transaction processing in PHP, and what are the precautions to implement ADODB transaction processing in PHP. The following is a practical case, let's take a look.

1. Code

adodb.inc.php can be downloaded from the official website http://adodb.sourceforge.net/.

Or click here

Download from this site.

conn.php:

<?php
  include_once (&#39;../adodb5/adodb.inc.php&#39;);
  $conn = ADONewConnection(&#39;mysql&#39;);
  $conn -> PConnect('localhost','root','root','db_database14');
  $conn -> execute('set names gb2312');
?>
trans.php:

<!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=gb2312" />
<title>处理事务</title>
<style type="text/css">
<!--
body,td,th {
  font-size: 12px;
}
body {
  margin-left: 10px;
  margin-top: 10px;
  margin-right: 10px;
  margin-bottom: 10px;
}
-->
</style></head>
<body>
<?php
  include_once &#39;conn/conn.php&#39;;              //载入数据库链接文件
  $conn -> BeginTrans();                //开始事务处理
  $sql = 'delete from tb_object where id = 3';        //sql删除语句
  $rst = $conn -> execute($sql) or die('execute error: '.$conn -> ErrorMsg());     //执行删除语句
  $num = $conn -> Affected_rows();           //查看被更新的记录数
  if(false !== $rst){                 //如果$rst不为假
    if($num != 0){                 //如果$num不为0,说明删除成功
      $conn -> CommitTrans();           //执行提交
      echo '删除成功!';
      exit();
    }else{                   //如果$num为0,说明没有删除记录
      echo '没有数据,或数据已删除';
      exit();
    }
  }else{                     //如果发生意外
    $conn -> RollbackTrans();            //执行回滚操作
    echo '出现意外。';
  }
?>
</body>
</html>

2. Running results

successfully deleted!

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

How to dynamically add, modify, and delete JS arrays and JSON objects

How to use JS Inheritance and multiple inheritance

The above is the detailed content of How to implement ADODB transaction processing in PHP. For more information, please follow other related articles on the PHP Chinese website!

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