是否可以跨多个函数进行一个sql事务?

时间:2016-05-06 22:53:44

标签: php pdo

我在另一个函数中使用了一些函数来更新某些东西,删除一些并插入,现在我的问题,如果上面的2成功或1,其余的不会导致灾难性后果。因此,可以对所有3个函数使用1个事务...例如:

public static function do_stuff()
{
    //run sql in function
    SELF::function_sql_one_insert();

    SELF::function_sql_two_update();

    SELF::function_sql_three_delete();
}

像这样:

public static function test()
{
    SELF::function_sql_one_insert();

    SELF::function_sql_two_update();

    SELF::function_sql_three_delete();
}

public static function function_sql_one_insert()
{
    //sql to run
    $sql = "INSERT INTO table
            (
                fake_row_one,
                fake_row_two
            )
            VALUES
            (
                ?,
                ?
            )";

    //run sql
    $fake_insert = $database->prepare($sql);
    $fake_insert->execute("yeah", "okay");
}

public static function function_sql_two_update()
{
    //sql to run
    $sql = "UPDATE table
            SET fake_row_one = ?
            WHERE fake_row_two = ?";

    //run sql
    $fake_update = $database->prepare($sql);
    $fake_update->execute("blahblah", "okay");
}

public static function function_sql_three_delete()
{
    //sql to run
    $sql = "DELETE FROM TABLE
            WHERE fake_row_two = ?";

    //run sql
    $fake_delete = $database->prepare($sql);
    $fake_delete->execute("okay");
}

我想要实现的是,如果一个人失败了将所有这些恢复。这可能吗?如果不是我可以做什么,如果是的话,这有什么用处吗?

4 个答案:

答案 0 :(得分:1)

Php函数与数据库事务完全无关。这只是无关紧要的事情。

数据库事务仅绑定到数据库连接。因此,只要您的所有函数都使用相同的连接,就可以在事务中运行所有三个函数。

答案 1 :(得分:0)

您应该为此使用数据库事务。

基本上,

  1. 你开始交易,
  2. 您执行SQL查询
  3. 如果它在某处失败,则执行回滚,否则执行提交
  4. 但是有一些问题,比如某些sql语句会自行提交,所以请阅读您正在使用的数据库的所有官方文档。

    更多信息:http://php.net/manual/en/pdo.begintransaction.php

    但请记住,并非所有数据库都支持事务开始。 例如。 MySQL中的MyISAM表不支持事务。

    您可能希望将这些表转换为InnoDB,请参阅此处:How to convert all tables from MyISAM into InnoDB?

答案 2 :(得分:0)

我用这种方式:

class Teste {
public $mysqli;
public $erro = array();

        public function __construct(){
        $this->mysqli = new \mysqli(DB_HOST,DB_USERNAME,DB_PASSWORD,DATABASE);
        $this->mysqli->set_charset("utf8");
        }

        public function start_trans(){
             $this->mysqli->autocommit(false);
        }

        public function end_trans(){

            if(count($this->erro) == 0){
            $this->mysqli->commit();
            echo "success";
            } else {
            $this->mysqli->rollback();
            echo "error";
            }

        }


        public function example1(){

            $stmt = $this->mysqli->query("insert into veiculos (placa, modelo) values (1,1)");

            if(!$stmt){
            $this->erro[] = "Erro #143309082017 <code>" . $this->mysqli->error . "</code>";
            }

            return (count($this->erro) < 1)? true : false;

        }


        public function example2(){

            $stmt = $this->mysqli->query("insert into veiculos (placa, modelo) values (2,2)");

            if(!$stmt){
            $this->erro[] = "Erro #143309082017 <code>" . $this->mysqli->error . "</code>";
            }

            return (count($this->erro) < 1)? true : false;

        }


        public function example3(){

            $this->mysqli->autocommit(false);

            $stmt = $this->mysqli->query("insert into veiculos (placa, modelo) values (3,3)");

            if(!$stmt){
            $this->erro[] = "Erro #143309082017 <code>" . $this->mysqli->error . "</code>";
            }

            return (count($this->erro) < 1)? true : false;

        }
}


        $action = new Teste;

        $action->start_trans();

        $action->example1();
        $action->example2();
        $action->example3();

        $action->end_trans();

答案 3 :(得分:-1)

也许你可以尝试这样的事情:

$working = true;
try
{
     SELF::function_sql_one_insert();
} catch (Exception $e)
{
    if($e != "")
        $working = false;
}

尝试使用所有功能。如果$ working为true,则可以执行所有命令。

相关问题