Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Begin Transaction not defined

Tags:

sql

php

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

    ?>
like image 381
Nigel How Avatar asked Aug 12 '13 08:08

Nigel How


People also ask

Why does my transaction request has no corresponding Begin Transaction?

If you’re receiving error Msg 3902, Level 16, which reads “The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION”, it’s probably because you’ve got a stray COMMIT statement.

What happens if you begin and then not commit a transaction?

Therefore, if you begin and then not commit, it will be rolled back on connection close (as the transaction was broken off without marking as complete). That's how it should be, but it's not always the case. ...such as mySQL's MyISAM, which doesn't support transactions, sure. depends on the isolation level of the incomming transaction.

What is Begin Transaction in database?

BEGIN TRANSACTION increments @@TRANCOUNT by 1. BEGIN TRANSACTION represents a point at which the data referenced by a connection is logically and physically consistent. If errors are encountered, all data modifications made after the BEGIN TRANSACTION can be rolled back to return the data to this known state of consistency.

When does a local transaction become a distributed transaction?

General Remarks. The local transaction started by the BEGIN TRANSACTION statement is escalated to a distributed transaction if the following actions are performed before the statement is committed or rolled back: An INSERT, DELETE, or UPDATE statement that references a remote table on a linked server is executed.


1 Answers

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);
like image 103
Leopoldo Sanczyk Avatar answered Oct 04 '22 13:10

Leopoldo Sanczyk