这里我默认认为你使用的是PDO吧。
官方文档是这样说的:
When the script ends or when a connection is about to be closed, if you have an outstanding transaction, PDO will automatically roll it back. This is a safety measure to help avoid inconsistency in the cases where the script terminates unexpectedly--if you didn't explicitly commit the transaction, then it is assumed that something went awry, so the rollback is performed for the safety of your data.
When the script ends or when a connection is about to be closed
意思就是说,当脚本结束(包括正常结束或者异常结束).
PDO will automatically roll it back
PDO会自动回滚。
文档已经说的很清楚了。
如果你不是用的PDO驱动,可查看相关驱动文档,或者在问题中说清楚用的什么驱动。
顺便一提,之前的mysql_connect()函数官方已经不建议再使用了(was deprecated)。