如何在Wordpress中使用MySQL事务? [英] How to use mysql transaction in wordpress?

查看:162
本文介绍了如何在Wordpress中使用MySQL事务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在wordpress中使用mysql事务?我要删除10个孩子,如果一个孩子处于活动状态,则总删除将回滚.

How to use mysql transaction in wordpress? I want delete 10 child, if one is active, the total delete will be rollback.

推荐答案

我从未尝试过,也没有什么特别的,但这只是关于运行查询(例如,在START TRANSACTION之后运行查询并使用COMMITROLLBACK取决于结果):

I've never tried it and there is nothing extra ordinary but it's just about running a query like (Run your queries after START TRANSACTION and use COMMIT or ROLLBACK depending on the result) :

mysql_query('START TRANSACTION');
$res1 = mysql_query('query1');
$res2 = mysql_query('query2');
If ( $res1 && $res2 ) {
    mysql_query('COMMIT'); // commits all queries
} else {
    mysql_query('ROLLBACK'); // rollbacks everything
}

因此,可以使用类似这样的方式将其转换为wordpress

So, it could be converted to wordpress, using something like this

$wpdb->query('START TRANSACTION');
$result1 = $wpdb->delete( $table, $where, $where_format = null );
$resul2 = $wpdb->delete( $table, $where, $where_format = null );
if($result1 && $result2) {
    $wpdb->query('COMMIT'); // if you come here then well done
}
else {
    $wpdb->query('ROLLBACK'); // // something went wrong, Rollback
}

您也可以使用try catch,例如此答案,(不是WordPress,但想法相同).您可以在 Codex 上阅读有关$wpdb查询功能(querydelete)的更多信息.

You may also use try catch like this answer, (not WordPress but same idea). You can read more about $wpdb query functions (query and delete) on Codex.

MySQL的默认MyISAM存储引擎不支持交易, 所以这不是一个选择.如果要使用交易,请确保所有 您的表被定义为InnoDB.

MySQL’s default MyISAM storage engine does not support transactions, so it’s not an option. If you want to use transactions, ensure all your tables are defined as InnoDB.

这篇关于如何在Wordpress中使用MySQL事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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