Mysql statement submitted by a PHPPDO transaction
$ Db = new PDO ('MySQL: host = '. $ cfg ['hostanme']. '; dbname = '. $ cfg ['dbname'], $ cfg ['username'], $ cfg ['password']); $ db-> exec ("set character_set_client = utf8 "); $ db-> exec ("SET character_set_results = utf8"); $ db-> exec ("SET character_set_connection = utf8"); $ db-> query ("BEGIN "); // start the transaction $ calories = "sa al-"; $…… = $ db-> prepare ('Insert INTO 'test _ user' values (null, 1,: calories ); '); $ Something-> execute (array (': calories '=> $ calories )); $ sths = $ db-> prepare ('Insert INTO 'test _ users' values (null, 1,: calories); '); $ sths-> execute (array (': calories '=> $ calories); // $ lastid = $ db-> lastInsertId (); $ sd = $ Something-> rowCount ();
I would like to ask if I can only execute one SQL statement at a time to process the transaction in this way, and then judge? If I have a lot of SQL statements, such as more than 10, isn't it necessary to write more than 10 statements and judge more than 10 times? Can I execute multiple SQL transactions at a time? In addition, in mysql, how do I write transactions in the stored procedure? How can PHP be called?
Reply to discussion (solution)
Drop procedure if exists pro_rep_shadow_rs; create procedure pro_rep_shadow_rs (out rtn int) begin -- IF an exception occurs, rollback declare exit handler for sqlexception ROLLBACK; -- start transaction; insert into test_user values (NULL, 1, 'Ah, '); insert into test_user VALUES (NULL, 23, 'sdsd', ss ); -- intentionally written incorrectly -- no running exception, commit transaction commit; -- set the return value to 1 set rtn = 1; end;
It is much easier to use stored procedures to execute transactions.