Home > Backend Development > PHP Tutorial > PHP mysql transaction rollback operation example_PHP tutorial

PHP mysql transaction rollback operation example_PHP tutorial

WBOY
Release: 2016-07-13 17:06:37
Original
861 people have browsed it

This article will introduce you to an example of PHP mysql transaction rollback operation. Friends who need to know about transaction rollback can refer to it. The operation method is very simple.

Only INNODB and BDB type data tables in MYSQL can support transaction processing! Other types are not supported!

 代码如下 复制代码
public function insertUser ($userArray){
  foreach ($userArray as $key => $value) {
   @$field  .= "$key,";
   @$content .= "'$value',";
  } 
  $field  = ereg_replace(',$', '', $field);
  $content = ereg_replace(',$', '', $content);
 
  $db = db_connect();  //连接数据库
 
  $db->autocommit(FALSE); //设置为非自动提交——事务处理
 
  $sql1  = "INSERT INTO t_user (".$field.") VALUES (".$content.")";
  $result1 = $db->query($sql1);
 
  $sql2  = "INSERT INTO t_userpost (f_username) VALUES ('".$userArray['f_username']."')";
  $result2 = $db->query($sql2);
 
  if ($result1 && $result2) {
   $db->commit();  //全部成功,提交执行结果
   echo '提交';
  } else {
   $db->rollback(); //有任何错误发生,回滚并取消执行结果
   echo '回滚';
  }
  $db->close();
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/630678.htmlTechArticleThis article will introduce you to an example of PHP mysql transaction rollback operation. Friends who need to know about transaction rollback can do it refer to. The operation method is very simple. There are only INNODB and BDB type data in MYSQL...
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