{"id":851,"date":"2012-07-30T09:22:32","date_gmt":"2012-07-30T09:22:32","guid":{"rendered":"http:\/\/www.withinweb.com\/info\/?p=851"},"modified":"2012-07-30T09:22:32","modified_gmt":"2012-07-30T09:22:32","slug":"simple-transaction-example-using-php-and-mysql_query","status":"publish","type":"post","link":"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/","title":{"rendered":"Simple transaction example using PHP and mysql_query"},"content":{"rendered":"<p>&lt;?php<br \/>\n\/\/ A simple transaction example using PHP and mysql_query<\/p>\n<p>\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\n@mysql_connect(&#8220;localhost&#8221;,&#8221;myusername&#8221;, &#8220;mypassword&#8221;) or die(mysql_error());<br \/>\n@mysql_select_db(&#8220;mydatabase&#8221;) or die(mysql_error());<br \/>\n$query = &#8220;INSERT INTO mytable (firstname, lastname) values (&#8216;Fred&#8217;,&#8217;Bloggs&#8217;)&#8221;;<\/p>\n<p>begin(); \/\/ begin transaction<br \/>\n$result = @mysql_query($query);<\/p>\n<p>if(!$result) {<br \/>\nrollback(); \/\/ failed so roll back transaction<br \/>\necho &#8220;Rolled back&#8221;;<br \/>\nexit;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\ncommit(); \/\/ Committ transaction<br \/>\necho &#8220;Transaction completed&#8221;;<br \/>\n}<br \/>\n\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<\/p>\n<p>\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nfunction begin() {<br \/>\n@mysql_query(&#8220;BEGIN&#8221;);<br \/>\n}<\/p>\n<p>\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nfunction commit() {<br \/>\n@mysql_query(&#8220;COMMIT&#8221;);<br \/>\n}<\/p>\n<p>\/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br \/>\nfunction rollback() {<br \/>\n@mysql_query(&#8220;ROLLBACK&#8221;);<br \/>\n}<\/p>\n<p>?&gt;<\/p>\n<p>The above code illustrates the use of transactions with mysql_query code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/\/ A simple transaction example using PHP and mysql_query \/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; @mysql_connect(&#8220;localhost&#8221;,&#8221;myusername&#8221;, &#8220;mypassword&#8221;) or die(mysql_error()); @mysql_select_db(&#8220;mydatabase&#8221;) or die(mysql_error()); $query = &#8220;INSERT INTO mytable (firstname, lastname) values (&#8216;Fred&#8217;,&#8217;Bloggs&#8217;)&#8221;; begin(); \/\/ begin transaction $result = @mysql_query($query); if(!$result) { rollback(); \/\/ failed so<span class=\"ellipsis\">&hellip;<\/span><\/p>\n<div class=\"read-more\"><a href=\"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/\">Read more <span class=\"screen-reader-text\">Simple transaction example using PHP and mysql_query<\/span><span class=\"meta-nav\"> &#8250;<\/span><\/a><\/div>\n<p><!-- end of .read-more --><\/p>\n","protected":false},"author":40,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,10],"tags":[],"class_list":["post-851","post","type-post","status-publish","format-standard","hentry","category-general-php","category-sql_databases_and_queries"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Simple transaction example using PHP and mysql_query - PHP Web Applications<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Simple transaction example using PHP and mysql_query - PHP Web Applications\" \/>\n<meta property=\"og:description\" content=\"&lt;?php \/\/ A simple transaction example using PHP and mysql_query \/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; @mysql_connect(&#8220;localhost&#8221;,&#8221;myusername&#8221;, &#8220;mypassword&#8221;) or die(mysql_error()); @mysql_select_db(&#8220;mydatabase&#8221;) or die(mysql_error()); $query = &#8220;INSERT INTO mytable (firstname, lastname) values (&#8216;Fred&#8217;,&#8217;Bloggs&#8217;)&#8221;; begin(); \/\/ begin transaction $result = @mysql_query($query); if(!$result) { rollback(); \/\/ failed so&hellip;Read more Simple transaction example using PHP and mysql_query &#8250;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/\" \/>\n<meta property=\"og:site_name\" content=\"PHP Web Applications\" \/>\n<meta property=\"article:published_time\" content=\"2012-07-30T09:22:32+00:00\" \/>\n<meta name=\"author\" content=\"paulv\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"paulv\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-transaction-example-using-php-and-mysql_query\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-transaction-example-using-php-and-mysql_query\\\/\"},\"author\":{\"name\":\"paulv\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"headline\":\"Simple transaction example using PHP and mysql_query\",\"datePublished\":\"2012-07-30T09:22:32+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-transaction-example-using-php-and-mysql_query\\\/\"},\"wordCount\":104,\"commentCount\":0,\"articleSection\":[\"General PHP\",\"SQL databases\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-transaction-example-using-php-and-mysql_query\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-transaction-example-using-php-and-mysql_query\\\/\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-transaction-example-using-php-and-mysql_query\\\/\",\"name\":\"Simple transaction example using PHP and mysql_query - PHP Web Applications\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\"},\"datePublished\":\"2012-07-30T09:22:32+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-transaction-example-using-php-and-mysql_query\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-transaction-example-using-php-and-mysql_query\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/simple-transaction-example-using-php-and-mysql_query\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Simple transaction example using PHP and mysql_query\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#website\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/\",\"name\":\"PHP Web Applications\",\"description\":\"Information and support for products of WithinWeb.com\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/#\\\/schema\\\/person\\\/04da5531c302d55ffcd777fe81dbb93c\",\"name\":\"paulv\",\"url\":\"https:\\\/\\\/www.withinweb.com\\\/info\\\/author\\\/paulv\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple transaction example using PHP and mysql_query - PHP Web Applications","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/","og_locale":"en_US","og_type":"article","og_title":"Simple transaction example using PHP and mysql_query - PHP Web Applications","og_description":"&lt;?php \/\/ A simple transaction example using PHP and mysql_query \/\/&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; @mysql_connect(&#8220;localhost&#8221;,&#8221;myusername&#8221;, &#8220;mypassword&#8221;) or die(mysql_error()); @mysql_select_db(&#8220;mydatabase&#8221;) or die(mysql_error()); $query = &#8220;INSERT INTO mytable (firstname, lastname) values (&#8216;Fred&#8217;,&#8217;Bloggs&#8217;)&#8221;; begin(); \/\/ begin transaction $result = @mysql_query($query); if(!$result) { rollback(); \/\/ failed so&hellip;Read more Simple transaction example using PHP and mysql_query &#8250;","og_url":"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/","og_site_name":"PHP Web Applications","article_published_time":"2012-07-30T09:22:32+00:00","author":"paulv","twitter_card":"summary_large_image","twitter_misc":{"Written by":"paulv"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/#article","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/"},"author":{"name":"paulv","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"headline":"Simple transaction example using PHP and mysql_query","datePublished":"2012-07-30T09:22:32+00:00","mainEntityOfPage":{"@id":"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/"},"wordCount":104,"commentCount":0,"articleSection":["General PHP","SQL databases"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/","url":"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/","name":"Simple transaction example using PHP and mysql_query - PHP Web Applications","isPartOf":{"@id":"https:\/\/www.withinweb.com\/info\/#website"},"datePublished":"2012-07-30T09:22:32+00:00","author":{"@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c"},"breadcrumb":{"@id":"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.withinweb.com\/info\/simple-transaction-example-using-php-and-mysql_query\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.withinweb.com\/info\/"},{"@type":"ListItem","position":2,"name":"Simple transaction example using PHP and mysql_query"}]},{"@type":"WebSite","@id":"https:\/\/www.withinweb.com\/info\/#website","url":"https:\/\/www.withinweb.com\/info\/","name":"PHP Web Applications","description":"Information and support for products of WithinWeb.com","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.withinweb.com\/info\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.withinweb.com\/info\/#\/schema\/person\/04da5531c302d55ffcd777fe81dbb93c","name":"paulv","url":"https:\/\/www.withinweb.com\/info\/author\/paulv\/"}]}},"_links":{"self":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/851","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/users\/40"}],"replies":[{"embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/comments?post=851"}],"version-history":[{"count":2,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/851\/revisions"}],"predecessor-version":[{"id":853,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/posts\/851\/revisions\/853"}],"wp:attachment":[{"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/media?parent=851"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/categories?post=851"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.withinweb.com\/info\/wp-json\/wp\/v2\/tags?post=851"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}