{"id":2212,"date":"2017-03-25T14:57:12","date_gmt":"2017-03-25T09:27:12","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=2212"},"modified":"2017-03-28T16:28:10","modified_gmt":"2017-03-28T10:58:10","slug":"php-mysql-transactions-examples","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/php-mysql-transactions-examples\/","title":{"rendered":"[ Solved -6 Answers ] PHP &#8211; MySQL- PHP + MySQL transactions examples"},"content":{"rendered":"<p><label class=\"label label-info\">SOLUTION :1<\/label><\/p>\n<ul>\n<li>We generally use when working with transactions looks like this (semi-pseudo-code):<\/li>\n<\/ul>\n[pastacode lang=\u201dsql\u201d manual=\u201dtry%20%7B%0A%20%20%20%20%2F%2F%20First%20of%20all%2C%20let\u2019s%20begin%20a%20transaction%0A%20%20%20%20%24db-%3EbeginTransaction()%3B%0A%0A%20%20%20%20%2F%2F%20A%20set%20of%20queries%3B%20if%20one%20fails%2C%20an%20exception%20should%20be%20thrown%0A%20%20%20%20%24db-%3Equery(\u2018first%20query\u2019)%3B%0A%20%20%20%20%24db-%3Equery(\u2018second%20query\u2019)%3B%0A%20%20%20%20%24db-%3Equery(\u2018third%20query\u2019)%3B%0A%0A%20%20%20%20%2F%2F%20If%20we%20arrive%20here%2C%20it%20means%20that%20no%20exception%20was%20thrown%0A%20%20%20%20%2F%2F%20i.e.%20no%20query%20has%20failed%2C%20and%20we%20can%20commit%20the%20transaction%0A%20%20%20%20%24db-%3Ecommit()%3B%0A%7D%20catch%20(Exception%20%24e)%20%7B%0A%20%20%20%20%2F%2F%20An%20exception%20has%20been%20thrown%0A%20%20%20%20%2F%2F%20We%20must%20rollback%20the%20transaction%0A%20%20%20%20%24db-%3Erollback()%3B%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p>Note that, with this idea, if a query fails, an Exception must be thrown:<\/p>\n<p>PDO can do that, depending on how you configure it<\/p>\n<ul>\n<li>See PDO::setAttribute<\/li>\n<li>and PDO::ATTR_ERRMODE and PDO::ERRMODE_EXCEPTION<\/li>\n<\/ul>\n<p>else, with some other API, you might have to test the result of the function used to execute a query, and throw an exception yourself.<\/p>\n<p>You cannot just put an instruction somewhere and have transactions done automatically: you still have to specific which group of queries must be executed in a transaction.<\/p>\n<p>For example, quite often you\u2019ll have a couple of queries before the transaction (before the begin) and another couple of queries after the transaction (after either commit or rollback) and you\u2019ll want those queries executed no matter what happened (or not) in the transaction.<\/p>\n<p><label class=\"label label-info\">SOLUTION :2<\/label><\/p>\n[pastacode lang=\u201dsql\u201d manual=\u201dmysql_query(%22START%20TRANSACTION%22)%3B%0A%0A%24a1%20%3D%20mysql_query(%22INSERT%20INTO%20rarara%20(l_id)%20VALUES(\u20181\u2019)%22)%3B%0A%24a2%20%3D%20mysql_query(%22INSERT%20INTO%20rarara%20(l_id)%20VALUES(\u20182\u2019)%22)%3B%0A%0Aif%20(%24a1%20and%20%24a2)%20%7B%0A%20%20%20%20mysql_query(%22COMMIT%22)%3B%0A%7D%20else%20%7B%20%20%20%20%20%20%20%20%0A%20%20%20%20mysql_query(%22ROLLBACK%22)%3B%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION :3<\/label><\/p>\n<ul>\n<li>we made a function to get a vector of queries and do a transaction, maybe someone will find out it useful:<\/li>\n<\/ul>\n[pastacode lang=\u201dsql\u201d manual=\u201dfunction%20transaction%20(%24con%2C%20%24Q)%7B%0A%20%20%20%20%20%20%20%20mysqli_query(%24con%2C%20%22START%20TRANSACTION%22)%3B%0A%0A%20%20%20%20%20%20%20%20for%20(%24i%20%3D%200%3B%20%24i%20%3C%20count%20(%24Q)%3B%20%24i%2B%2B)%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(!mysqli_query%20(%24con%2C%20%24Q%5B%24i%5D))%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20echo%20\u2019Error!%20Info%3A%20%3C\u2019%20.%20mysqli_error%20(%24con)%20.%20\u2019%3E%20Query%3A%20%3C\u2019%20.%20%24Q%5B%24i%5D%20.%20\u2019%3E\u2019%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%20%20%20%0A%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20if%20(%24i%20%3D%3D%20count%20(%24Q))%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20mysqli_query(%24con%2C%20%22COMMIT%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%201%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20else%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20mysqli_query(%24con%2C%20%22ROLLBACK%22)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20return%200%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION :4<\/label><\/p>\n<ul>\n<li>One more procedural style example with mysqli_multi_query, assumes $query is filled with semicolon-separated statements.<\/li>\n<\/ul>\n[pastacode lang=\u201dsql\u201d manual=\u201dmysqli_begin_transaction%20(%24link)%3B%0A%0Afor%20(mysqli_multi_query%20(%24link%2C%20%24query)%3B%0A%20%20%20%20mysqli_more_results%20(%24link)%3B%0A%20%20%20%20mysqli_next_result%20(%24link)%20)%3B%0A%0A!%20mysqli_errno%20(%24link)%20%3F%0A%20%20%20%20mysqli_commit%20(%24link)%20%3A%20mysqli_rollback%20(%24link)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><label class=\"label label-info\">SOLUTION :5<\/label><\/p>\n<ul>\n<li>check which storage engine you are using. If it is MyISAM, then Transaction(\u2018COMMIT\u2019,\u2019ROLLBACK\u2019) will not be supported because only the InnoDB storage engine, not MyISAM, supports transactions.<\/li>\n<\/ul>\n<p><label class=\"label label-info\">SOLUTION :6<\/label><\/p>\n<p><b>PHP MySQL transaction example<\/b><\/p>\n<ul>\n<li>First, execute the following statement to create the accounts table:<\/li>\n<\/ul>\n[pastacode lang=\u201dsql\u201d manual=\u201dCREATE%20TABLE%20accounts%20(%0A%20%20%20%20id%20%20%20%20%20INT%20AUTO_INCREMENT%20PRIMARY%20KEY%2C%0A%20%20%20%20name%20%20%20VARCHAR%20(50)%20%20%20%20NOT%20NULL%2C%0A%20%20%20%20amount%20DECIMAL%20(19%2C%204)%20NOT%20NULL%0A)%3B%0ASecond%2C%20insert%20two%20rows%20into%20the%20accounts%20table%3A%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p style=\"top: 90px;\">[ad type=\u201dbanner\u201d]\n<ul>\n<li>Second, insert two rows into the accounts table:<\/li>\n<\/ul>\n[pastacode lang=\u201dsql\u201d manual=\u201dINSERT%20INTO%20accounts(name%2Camount)%0AVALUES(\u2018John\u2019%2C25000)%2C%0A%20%20%20%20%20%20(\u2018Mary\u2019%2C95000)%3B%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<ul>\n<li>Third, query the accounts table:<\/li>\n<\/ul>\n<p>SELECT * FROM accounts;<\/p>\n<p><img decoding=\"async\" class=\"wp-image-2216 size-full aligncenter\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/03\/Picture1-5.png\" alt=\"\" width=\"188\" height=\"62\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SOLUTION :1 We generally use when working with transactions looks like this (semi-pseudo-code): [pastacode lang=\u201dsql\u201d manual=\u201dtry%20%7B%0A%20%20%20%20%2F%2F%20First%20of%20all%2C%20let\u2019s%20begin%20a%20transaction%0A%20%20%20%20%24db-%3EbeginTransaction()%3B%0A%0A%20%20%20%20%2F%2F%20A%20set%20of%20queries%3B%20if%20one%20fails%2C%20an%20exception%20should%20be%20thrown%0A%20%20%20%20%24db-%3Equery(\u2018first%20query\u2019)%3B%0A%20%20%20%20%24db-%3Equery(\u2018second%20query\u2019)%3B%0A%20%20%20%20%24db-%3Equery(\u2018third%20query\u2019)%3B%0A%0A%20%20%20%20%2F%2F%20If%20we%20arrive%20here%2C%20it%20means%20that%20no%20exception%20was%20thrown%0A%20%20%20%20%2F%2F%20i.e.%20no%20query%20has%20failed%2C%20and%20we%20can%20commit%20the%20transaction%0A%20%20%20%20%24db-%3Ecommit()%3B%0A%7D%20catch%20(Exception%20%24e)%20%7B%0A%20%20%20%20%2F%2F%20An%20exception%20has%20been%20thrown%0A%20%20%20%20%2F%2F%20We%20must%20rollback%20the%20transaction%0A%20%20%20%20%24db-%3Erollback()%3B%0A%7D%0A\u201d message=\u201djQuery Code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/] [ad type=\u201dbanner\u201d] Note that, with this idea, if a query fails, an Exception must be thrown: PDO can do that, depending on how you configure it See PDO::setAttribute and PDO::ATTR_ERRMODE and PDO::ERRMODE_EXCEPTION else, with some [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27,25],"tags":[4833,4840,3350,4832,1184,4835,4837,4838,4841,4834,4839,4842,4836],"class_list":["post-2212","post","type-post","status-publish","format-standard","hentry","category-mysql","category-php","tag-can-i-get-mysql-to-rollback-a-transaction-if-i-disconnect-without-committing","tag-could-simultaneous-transactions-interfere-with-each-other-phpmysql","tag-how-can-i-prevent-sql-injection-in-php","tag-how-do-i-use-mysql-transactions-in-php","tag-in-php","tag-mysql-transactions-question","tag-mysql-transactions-with-php-queue-statements","tag-php-mysql-session-transaction","tag-php-mysql-transaction-rollback-doubts","tag-rather-than-commit-it","tag-transactions-mysql-and-php","tag-using-mysql-variable-with-php-variable-within-a-transaction","tag-with-php"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/2212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=2212"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/2212\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=2212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=2212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=2212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}