5

开始事务在 mysql 中未定义。我实际上使用它在我的代码中运行多个查询以将一行从一个表移动到另一个表。很多帮助将不胜感激。好的,我的问题是,为什么我的 Begin_transaction() 没有定义?

<?php 
    If(isset($trade_id)){
            $trade_id= $_GET['trade_id'];
    }
    require_once('connect.php');
    $mysqli = new mysqli($database_hostname, $database_username, $database_password, $database_name) or exit("Error connecting to database");
    try {
        // First of all, let's begin a transaction
        $mysqli->begin_transaction();

        // A set of queries; if one fails, an exception should be thrown
        $mysqli->query("INSERT INTO `trade_history1` (session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, profitandloss, dateclose, close)
        SELECT session_id, trade_id, selection, date, type, size, bidprice, offerprice, stoploss, takeprofit, profitandloss, dateclose, close
        FROM `opentrades`
        WHERE `trade_id` = " . $tradeid);
        $mysqli->query("DELETE FROM `opentrades` WHERE `trade_id` = " . $trade_id);

        // If we arrive here, it means that no exception was thrown
        // i.e. no query has failed, and we can commit the transaction
        $mysqli->commit();
        $_SESSION['message'] = 'Successfully deleted';
    } catch (Exception $e) {
        // An exception has been thrown
        // We must rollback the transaction
        $_SESSION['message'] = 'Unable to delete';
        $mysqli->rollback();
    }
    $mysqli->close();

            // if we successfully delete this, we 
            if ($successfullyDeleted) {
                $_SESSION['message'] = 'Successfully deleted';
            } else {
                $_SESSION['message'] = 'Unable to delete';
            }

            header('Location: js.php');

    ?>
4

3 回答 3

7

PHP 手册mysqli::begin_transaction需要PHP 5.5.0或更高版本(http://php.net/manual/en/mysqli.begin-transaction.php)。

但是您可以在PHP 5.x中使用mysqli::autocommit ( http://php.net/manual/en/mysqli.autocommit.php ) :

//Begin transaction
$mysqli->autocommit(FALSE);
...
//End transaction and auto commit
$mysqli->autocommit(TRUE);
于 2014-10-23T02:26:14.817 回答
5

$mysqli->begin_transaction();一定是$mysqli->autocommit(FALSE);

检查这个

http://www.php.net/manual/en/mysqli.commit.php

于 2013-08-12T08:59:29.057 回答
0

您可以使用我测试过的这段代码。

面向对象风格

$cid->multi_query('start transaction;');
$cid->multi_query("insert into demo values('1','Pankaj','9031251290')");
$cid->multi_query('commit;');

程序风格

mysqli_query($link, "start transaction;");
mysqli_query($link, "INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)");
mysqli_query($link, "commit;");
于 2015-10-20T15:14:59.263 回答