Mysql statement submitted by a PHPPDO transaction

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.