2

QUERY 1:

$link = mysql_connect('localhost', 'root', '');
mysql_select_db('rems', $link);
mysql_query('SET AUTOCOMMIT=0; START TRANSACTION', $link);
mysql_query('DELETE FROM admins WHERE admin_id=4', $link);
mysql_query('ROLLBACK; SET AUTOCOMMIT=1', $link);

QUERY 2:

$link = mysql_connect('localhost', 'root', '');
mysql_select_db('rems', $link);
mysql_query('SET AUTOCOMMIT=0;START TRANSACTION;
    DELETE FROM admins WHERE admin_id=4;
    ROLLBACK; SET AUTOCOMMIT=1', $link);

From the above two queries the first one does not execute properly (transaction does not work) because of executing the queries separately by calling the mysql_query functions multiple times. But i need it do be done by this way. That is i need the result by the first way (calling mysql_query function several times for a single transaction)

Any IDEA please???

5
  • Why the requirement of autocommit & start in 1 query? Seems silly, and moreover: impossible with the standard mysql module. Commented Jul 19, 2010 at 11:27
  • And the ROLLBACK and SET AUTOCOMMIT=1 in the last statement seem similarly ill-advised. When you say it doesn't work, what exactly is happening? Are you getting an error? Or is nothing happening? Note that since you are rolling back the transaction, you expect nothing to happen. Commented Jul 19, 2010 at 11:29
  • 1
    To make sure: Do you use InnoDB as database engine? Only this supports transactions. Commented Jul 19, 2010 at 11:31
  • @Wrikken & Hooper: The idea of the technique of setting AUTOCOMMIT at the begining and ending of the query has been collected from the great PHP Framework CodeIgniter. @Hooper: As the transaction has ROLLED BACK so the QUERY should not affect as the result. But the row deleted if i use QUERY1 method and not delete if i use QUERY2. @Kling: I am using InnoDB. If i was not then no one of the above would work. But QUERY2 is working properly :) Commented Jul 19, 2010 at 11:49
  • QUERY2 does NOT work properly: the ENTIRE query FAILS (including your DELETE statement). Hence no actual delete, but that has nothing to do with a ROLLBACK, but a general error (try it: COMMIT there won't work) Commented Jul 19, 2010 at 12:51

1 Answer 1

5

With the standard mysql module, either call every query seperately:

mysql_query('SET AUTOCOMMIT=0');
mysql_query('START TRANSACTION');
mysql_query('DELETE FROM admins WHERE admin_id=4');
mysql_query('ROLLBACK');//nothing will be done, I assume it's for testing
mysql_query('SET AUTOCOMMIT=1');

Or create a procedure first:

DELIMITER //
CREATE PROCEDURE weirdrolledbackdelete (IN oid INTEGER)
BEGIN
SET AUTOCOMMIT=0;
START TRANSACTION;
DELETE FROM admins WHERE id = oid;
ROLLBACK;
SET AUTOCOMMIT=1;
END;//

So you can use it later:

mysql_query('CALL weirdrolledbackdelete(4);');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot!!! It works!!! The mistake i did was calling two queries from a single mysql_queryfunction call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.