Does mysql_query commit everything

和自甴很熟 提交于 2019-12-24 03:02:12

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!