开始交易未定义

时间:2013-08-12 08:54:44

标签: php sql

mysql中未定义begin事务。我实际上使用它在我的代码中运行多个查询来将一行从一个表移动到另一个表。很多帮助将不胜感激。好的,我的问题是,为什么我的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');

    ?>

3 个答案:

答案 0 :(得分:6)

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

但您可以使用 mysqli :: autocommit 代替(http://php.net/manual/en/mysqli.autocommit.php PHP 5.x

//Begin transaction
$mysqli->autocommit(FALSE);
...
//End transaction and auto commit
$mysqli->autocommit(TRUE);

答案 1 :(得分:4)

$mysqli->begin_transaction();必须为$mysqli->autocommit(FALSE);

检查

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

答案 2 :(得分:-2)

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

  

面向对象的风格

${PODS_ROOT}/Headers/Public/AFNetworking
${PODS_ROOT}/Headers/Public/Bolts
${PODS_ROOT}/Headers/Public/ISO8601DateFormatterValueTransformer
${PODS_ROOT}/Headers/Public/RKValueTransformers
${PODS_ROOT}/Headers/Public/RestKit 
${PODS_ROOT}/Headers/Public/SOCKit
${PODS_ROOT}/Headers/Public
  

程序风格

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