开发者

MySQL Transaction From PHP

开发者 https://www.devze.com 2023-01-07 17:20 出处:网络
QUERY 1: $link = mysql_connect(\'localhost\'开发者_开发百科, \'root\', \'\'); mysql_select_db(\'rems\', $link);

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


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

精彩评论

暂无评论...
验证码 换一张
取 消