简体   繁体   中英

Begin Transaction not defined

The begin transaction is undefined in mysql. Im actually using it to run multiple queries in my code to move a row from one table to another. Much help will be appreciated. Okay my question is , Why is my Begin_transaction() not defined?

<?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');

    ?>

PHP Manual says mysqli::begin_transaction needs PHP version 5.5.0 or upper ( http://php.net/manual/en/mysqli.begin-transaction.php ).

But you can use mysqli::autocommit instead ( http://php.net/manual/en/mysqli.autocommit.php ) with PHP 5.x :

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

$mysqli->begin_transaction(); must be $mysqli->autocommit(FALSE);

Check this

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

You can use this code i have tested.

Object oriented style

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

Procedural style

mysqli_query($link, "start transaction;");
mysqli_query($link, "INSERT INTO Language VALUES ('DEU', 'Bavarian', 'F', 11.2)");
mysqli_query($link, "commit;");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM