search
Homephp教程php手册Codeigniter框架的更新事务(transaction)BUG及解决方法

这篇文章主要介绍了Codeigniter框架的更新事务(transaction)BUG及解决方法,具体BUG和解决办法在文中有详细描述,需要的朋友可以参考下

由于ci事务判断出错回滚的条件是语句是否执行成功,而更新操作时,就算影响的条数为0,sql语句执行的结果过仍然为1,因为它执行成功了,只是影响的条数为0。

下面介绍解决这个问题的方法:

对于一次要执行许多的语句的事务

只需在更新操作下根据影响条数是否为0来决定是否会滚即可,下面假设第二条语句为更新操作。

复制代码 代码如下:


//采用 Codeigniter 事务的手动模式
    $this->db->trans_strict(FALSE);
    $this->db->trans_begin();
       
    $this->db->query('SELECT ...');//SELECT 操作无需特殊处理
    $this->db->query('INSERT ...');//INSERT 出错会有 Codeigniter 自动处理
       
    $this->db->query('UPDATE ...');
    if (!$this->db->affacted_rows()) {//上面的 UPDATE 失败则回滚
        $this->db->trans_rollback();
        //@todo 异常处理部分
        exit();//需要终止或跳出,以免下面的 SQL 代码继续执行!
    }
       
    $this->db->query('DELETE ...');
    if (!$this->db->affacted_rows()) {//上面的 DELETE 失败则回滚
        $this->db->trans_rollback();
        //@todo 异常处理部分
        exit();//需要终止或跳出,以免下面的 SQL 代码继续执行!
    }
       
    $this->db->query('SELECT ...');//SELECT 操作无需特殊处理
    $this->db->query('INSERT ...');//INSERT 出错会有 Codeigniter 自动处理
       
    if ($this->db->trans_status() === TRUE) {
        $this->db->trans_commit();
    } else {
        $this->db->trans_rollback();
        //@todo 异常处理部分
    }

如果一次执行的语句不是很多,,可以在最后做一下判断来决定回滚

若语句中没有更新操作,用自动事务就可以。

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools