views:

79

answers:

1

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???

+1  A: 

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);');
Wrikken
Thanks a lot!!! It works!!! The mistake i did was calling two queries from a single mysql_queryfunction call.
Saiful