简体   繁体   English

交易和PHP

[英]Transactions and 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 : 我读了一篇关于它的文章,它说:

" In PHP there are no such methods and the we must use the direct SQL. (In PHP PDO there are such methods.) " “在PHP中没有这样的方法,我们必须使用直接SQL。(在PHP PDO中有这样的方法。)”

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. 请让我知道,仅因为我从未在PDO上工作过,才有可能在SQL查询中嵌入开始事务和结束事务,而我唯一的想法是在一个SQL查询中对表进行多次插入。

Rough idea 粗略的想法

<?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!!!";
        }

That will vary based on the db that you use. 这将根据您使用的数据库而有所不同。 The mysql documentation page on the subject: http://dev.mysql.com/doc/refman/5.5/en/commit.html 关于该主题的mysql文档页面: http : //dev.mysql.com/doc/refman/5.5/en/commit.html

 START TRANSACTION;
 SELECT @A:=SUM(salary) FROM table1 WHERE type=1;
 UPDATE table2 SET summary=@A WHERE type=1;
 COMMIT;

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;

If you want to more infomation about this please check this link http://www.techonthenet.com/oracle/questions/insert_rows.php 如果您想了解更多有关此的信息,请检查此链接http://www.techonthenet.com/oracle/questions/insert_rows.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM