mysql_query是否提交所有内容 [英] Does mysql_query commit everything

查看:87
本文介绍了mysql_query是否提交所有内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在php中使用mysql扩展,我知道从PHP 5.5.0起它已被弃用,但是我已经使用该扩展编写了很多代码。
在我看来,像mysql_query提交查询,如果这样的话,则意味着它被设置为自动提交,如何设置为不自动提交。

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.

推荐答案

旧的mysql扩展没有专门用于事务控制的功能,但是您可以发出SQL语句来执行所需的操作。

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");

或在您的MySQL实例上全局更改它,以便更改所有会话的默认值:

Or change it globally on your MySQL instance so it changes the default for all sessions:

mysql_query("SET GLOBAL autocommit=0");

或者通过编辑/etc/my.cnf将其设置为在MySQL服务启动时更改全局设置:

Or set it to change the global setting upon MySQL service startup by editing /etc/my.cnf:

[mysqld]
autocommit=0

如果您使用MySQL 5.1,则必须做些不同的操作:

If you use MySQL 5.1, you have to do this slightly differently:

[mysqld]
init_connect='SET autocommit=0'

您可能已经知道,但是需要重复的一点是,如果您使用InnoDB表,则事务仅意味着任何意义。 MyISAM表不支持事务,因此无论您如何设置,它们总是自动提交的。与其他几个存储引擎(包括MEMORY和CSV)相同。

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.

这篇关于mysql_query是否提交所有内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆