问题
I am using mysql extension in php, I know it is deprecated as of PHP 5.5.0, but I have a lot of code allready written with the use of this extension. It seems to me like mysql_query commits the query, if so it means that it is set to autocomit, How to setup so it does not autocommit.
回答1:
The old mysql extension doesn't have functions specifically for transaction control, but you can issue SQL statements to do what you want.
You can implicitly turn off autocommit for the duration of one transaction simply by starting a transaction:
mysql_query("START TRANSACTION");
As soon as you COMMIT or ROLLBACK, the autocommit mode will return to the default.
mysql_query("COMMIT"); // or ROLLBACK
You can turn off autocommit for your whole session by setting a session variable:
mysql_query("SET autocommit=0");
Or change it globally on your MySQL instance so it changes the default for all sessions:
mysql_query("SET GLOBAL autocommit=0");
Or set it to change the global setting upon MySQL service startup by editing /etc/my.cnf:
[mysqld]
autocommit=0
If you use MySQL 5.1, you have to do this slightly differently:
[mysqld]
init_connect='SET autocommit=0'
You probably already know, but it bears repeating that transactions only mean anything if you're using InnoDB tables. MyISAM tables don't support transactions, so they're always autocommit regardless of your settings. Same with several other storage engines, including MEMORY and CSV.
来源:https://stackoverflow.com/questions/14782299/does-mysql-query-commit-everything