简体   繁体   中英

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.) "

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.

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

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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