PHP& mySQL:实现交易的简单代码-Commit&回滚 [英] PHP & mySQL: Simple code to implement Transaction - Commit & Rollback

查看:75
本文介绍了PHP& mySQL:实现交易的简单代码-Commit&回滚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的平台:

PHP& mySQL

PHP & mySQL

我的情况:

我正在尝试在我的代码中实现事务.我尝试遵循示例,但这并没有太大帮助.我正在运行3个查询,我想以一种方式编写事务,以便如果任何查询失败,则整个事务应回滚.我非常感谢一个简单,高效且非面向对象的PHP代码来实现此目标.预先谢谢你.

I am trying to implement transactions within my code. I tried to follow examples, but it's not much help. I am running 3 queries and I wanted to write a transaction in such a way so that if any of the query(ies) fail, the whole transaction should roll back. I would really appreciate a simple, efficient and non-object oriented PHP code to achieve this goal. Thank you in advance.

我的PHP代码:

//db_res calls a custom function that performs a mysql_query on the query
$res1 = db_res("SELECT c1, c2 FROM t1 WHERE c5 = 3");
$res2 = db_res("UPDATE t2 SET c1 = 5 WHERE c2 = 10");
$res3 = db_res("DELETE FROM t3 WHERE c1 = 20");

if( $res1 && $res2 && $res3 )
{
 //commit --- but how?
}
else
{
 //rollback --- but how?
}

推荐答案

您需要使用 mysqli扩展以使用此功能.

You need to use the mysqli extension to use this functionality.

请参阅: autocommit()

这篇关于PHP& mySQL:实现交易的简单代码-Commit&回滚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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