-1

我正在尝试通过转换为 mysqli 来转换一些旧代码。不幸的是,我无法弄清楚代码的哪一部分正在尝试执行,所以我不知道如何更改它。这似乎是使用原始 mysql 扩展的每个人都使用的标准安全检查,但我找不到任何人解释原因。这是原始代码:

function query($query = "", $transaction = FALSE)
{
    //
    // Remove any pre-existing queries
    //
    unset($this->query_result);

    if( $query != "" )
    {
        $this->num_queries++;
        if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )
        {
            $result = mysql_query("BEGIN", $this->db_connect_id);
            if(!$result)
            {
                return false;
            }
            $this->in_transaction = TRUE;
        }

        $this->query_result = mysql_query($query, $this->db_connect_id);
    }
    else
    {
        if( $transaction == END_TRANSACTION && $this->in_transaction )
        {
            $result = mysql_query("COMMIT", $this->db_connect_id);
        }
    }

    if( $this->query_result )
    {
        unset($this->row[$this->query_result]);
        unset($this->rowset[$this->query_result]);

        if( $transaction == END_TRANSACTION && $this->in_transaction )
        {
            $this->in_transaction = FALSE;

            if ( !mysql_query("COMMIT", $this->db_connect_id) )
            {
                mysql_query("ROLLBACK", $this->db_connect_id);
                return false;
            }
        }

        return $this->query_result;
    }
    else
    {
        if( $this->in_transaction )
        {
            mysql_query("ROLLBACK", $this->db_connect_id);
            $this->in_transaction = FALSE;
        }
        return false;
    }
}

我不知道他们在做什么

if( $transaction == BEGIN_TRANSACTION && !$this->in_transaction )

有人可以向我解释一下吗?

4

1 回答 1

1

标记BEGIN_TRANSACTIONEND_TRANSACTION是在其他地方定义的任意常量,以使代码更具可读性。基本上,这整个函数是 MySQL Transactions in 的实现mysql,它不直接支持它们。大部分代码只是为了确定事务是否已启动,以及是否提交或回滚。

您可以使用 、 和 来支持相同mysqli::begin_transaction()mysqli::commit()功能mysqli::rollback()

PHP 参考在这里

于 2013-09-16T00:33:07.827 回答