交易和 PHP [英] Transactions and PHP

查看:24
本文介绍了交易和 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试制作这样的功能,以确保单击时所有值都成功存储在数据库的多个表中.我读了一篇关于它的文章,它说:

I have been trying to make such functionality that will make sure that on click all the values are being stored in the multiple tables of the database successfully. I read an article about it and it says :

" 在 PHP 中没有这样的方法,我们必须使用直接的 SQL.(在 PHP PDO 中有这样的方法.)"

" In PHP there are no such methods and the we must use the direct SQL. (In PHP PDO there are such methods.) "

请告诉我是否可以在 sql 查询中嵌入开始事务和结束事务,因为我从未在 PDO 上工作过,我唯一的想法是在一个 SQL 查询中对表进行多次插入.

Kindly let me know is it possible to embed begin transaction and end transaction in an sql query only because i never worked on PDO and the only idea i have is to make multiple insertion to tables in one SQL query.

粗略的想法

<?php
$sql = " 
Begin Transaction
insert into .............
insert into ...........
End Transaction "
mysql_query($sql) ?>

<小时>

我找到的解决方案:


SOLUTION THAT I FOUND:

mysql_select_db($database_dany, $dany);
  if (mysql_query('BEGIN')) {
    if (
        mysql_query($query1)  &&
        mysql_query($query2)  &&
        mysql_query($query3) &&
        mysql_query($insertSQL) 
        )
        $result = mysql_query('COMMIT'); // both queries looked OK, save
    else
    {
        mysql_query('ROLLBACK'); // problems with queries, no changes
        echo "There are some problemo!!!";
        }

推荐答案

您可以使用以下语法插入多行:

You can insert multiple rows using the following syntax:

INSERT ALL
   INTO mytable (column1, column2, column3) VALUES ('val1.1', 'val1.2', 'val1.3')
   INTO mytable (column1, column2, column3) VALUES ('val2.1', 'val2.2', 'val2.3')
   INTO mytable (column1, column2, column3) VALUES ('val3.1', 'val3.2', 'val3.3')
SELECT * FROM dual;

如果您想了解更多相关信息,请查看此链接 http://www.techonthenet.com/oracle/questions/insert_rows.php

If you want to more infomation about this please check this link http://www.techonthenet.com/oracle/questions/insert_rows.php

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

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