{"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=&#8221;sql&#8221; manual=&#8221;try%20%7B%0A%20%20%20%20%2F%2F%20First%20of%20all%2C%20let&#8217;s%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(&#8216;first%20query&#8217;)%3B%0A%20%20%20%20%24db-%3Equery(&#8216;second%20query&#8217;)%3B%0A%20%20%20%20%24db-%3Equery(&#8216;third%20query&#8217;)%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&#8221; message=&#8221;jQuery Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\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&#8217;ll 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&#8217;ll 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=&#8221;sql&#8221; manual=&#8221;mysql_query(%22START%20TRANSACTION%22)%3B%0A%0A%24a1%20%3D%20mysql_query(%22INSERT%20INTO%20rarara%20(l_id)%20VALUES(&#8216;1&#8217;)%22)%3B%0A%24a2%20%3D%20mysql_query(%22INSERT%20INTO%20rarara%20(l_id)%20VALUES(&#8216;2&#8217;)%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&#8221; message=&#8221;jQuery Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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=&#8221;sql&#8221; manual=&#8221;function%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&#8217;Error!%20Info%3A%20%3C&#8217;%20.%20mysqli_error%20(%24con)%20.%20&#8217;%3E%20Query%3A%20%3C&#8217;%20.%20%24Q%5B%24i%5D%20.%20&#8217;%3E&#8217;%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&#8221; message=&#8221;jQuery Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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=&#8221;sql&#8221; manual=&#8221;mysqli_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&#8221; message=&#8221;jQuery Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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(&#8216;COMMIT&#8217;,&#8217;ROLLBACK&#8217;) 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=&#8221;sql&#8221; manual=&#8221;CREATE%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&#8221; message=&#8221;jQuery Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p style=\"top: 90px;\">[ad type=&#8221;banner&#8221;]\n<ul>\n<li>Second, insert two rows into the accounts table:<\/li>\n<\/ul>\n[pastacode lang=&#8221;sql&#8221; manual=&#8221;INSERT%20INTO%20accounts(name%2Camount)%0AVALUES(&#8216;John&#8217;%2C25000)%2C%0A%20%20%20%20%20%20(&#8216;Mary&#8217;%2C95000)%3B%0A&#8221; message=&#8221;jQuery Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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=&#8221;sql&#8221; manual=&#8221;try%20%7B%0A%20%20%20%20%2F%2F%20First%20of%20all%2C%20let&#8217;s%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(&#8216;first%20query&#8217;)%3B%0A%20%20%20%20%24db-%3Equery(&#8216;second%20query&#8217;)%3B%0A%20%20%20%20%24db-%3Equery(&#8216;third%20query&#8217;)%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&#8221; message=&#8221;jQuery Code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/] [ad type=&#8221;banner&#8221;] 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}]}}