<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Codrops &#187; rollback</title>
	<atom:link href="http://tympanus.net/codrops/tag/rollback/feed/" rel="self" type="application/rss+xml" />
	<link>http://tympanus.net/codrops</link>
	<description>Useful resources and inspiration for creative minds</description>
	<lastBuildDate>Mon, 06 Feb 2012 07:30:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Using MySQL transactions with PHP</title>
		<link>http://tympanus.net/codrops/2009/09/01/using-mysql-transactions-with-php/</link>
		<comments>http://tympanus.net/codrops/2009/09/01/using-mysql-transactions-with-php/#comments</comments>
		<pubDate>Tue, 01 Sep 2009 19:04:57 +0000</pubDate>
		<dc:creator>chadking</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[commit]]></category>
		<category><![CDATA[innodb]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[rollback]]></category>
		<category><![CDATA[transaction]]></category>

		<guid isPermaLink="false">http://tympanus.net/codrops/?p=162</guid>
		<description><![CDATA[The following is an example of using the transactional support in MySQL. Let&#8217;s assume we have two tables, USERTBL and EMAILTBL. Let&#8217;s consider the innodb engine, since it&#8217;s the most popular transaction storage engine. CREATE TABLE USERTBL( USERKEY                     int unsigned not null auto_increment, email                        [...]]]></description>
			<content:encoded><![CDATA[<p>The following is an example of using the transactional support in MySQL. Let&#8217;s assume we have two tables, USERTBL and EMAILTBL. Let&#8217;s consider the innodb engine, since it&#8217;s the most popular transaction storage engine.</p>
<pre class="brush: sql">CREATE TABLE USERTBL(
USERKEY                     int unsigned not null auto_increment,
email                          varchar(120) not null,
name                          varchar(100) not null,
primary key(USERKEY)
)type=INNODB DEFAULT CHARACTER SET utf8    COLLATE utf8_general_ci;

CREATE TABLE EMAILTBL(
EMAILKEY                     int unsigned not null auto_increment,
email                           varchar(120) not null,
primary key(EMAILKEY)
)type=INNODB DEFAULT CHARACTER SET utf8    COLLATE utf8_general_ci;</pre>
<p><span id="more-162"></span>Now, as an example, let&#8217;s say we need to insert a new record / new user in the USERTBL and after that we want to insert the user&#8217;s email in the table EMAILTBL. We only want to insert in the second table if the first insertion succeeded. Also, if the second insertion fails, we want to terminate the transaction, meaning that the first one would be undone.</p>
<ol>
<li>To start the transaction we need to set the autocommit to FALSE (<a title="php.net" href="http://us2.php.net/manual/en/mysqli.autocommit.php">mysqli_autocommit</a>)</li>
<li>To undo a transaction we use the ROLLBACK statement, which also undoes any change to the database made by the transaction and then terminates that transaction (<a title="php.net" href="http://us3.php.net/manual/en/mysqli.rollback.php">mysqli_rollback</a>)</li>
<li>To save all changes made in the transaction to the database we use the COMMIT statement. This also terminates the transaction (<a title="php.net" href="http://us3.php.net/manual/en/mysqli.commit.php">mysqli_commit</a>)</li>
</ol>
<p>The function dbProcessEmail in the following class snippet shows the three cases mentioned above</p>
<pre class="brush: php">&lt;?php
class DB{
private $link;

public function __construct(){
$this-&gt;link = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}
//...
public function dbProcessEmail($email,$name){
mysqli_autocommit($this-&gt;link,FALSE);
mysqli_query($this-&gt;link,"INSERT INTO USERTBL(USERKEY,email,name) VALUES('NULL','$email','$name')");
if(mysqli_errno($this-&gt;link)){
printf("transaction aborted: %s\n", mysqli-&gt;error);
mysqli_rollback($this-&gt;link);
return -1;
}
else{
mysqli_query($this-&gt;link,"INSERT INTO EMAILTBL(EMAILKEY,email) VALUES('NULL','$email')");
if(mysqli_errno($this-&gt;link)){
printf("transaction aborted: %s\n", mysqli-&gt;error);
mysqli_rollback($this-&gt;link);
return -1;
}
else{
printf("transaction succeeded\n");
mysqli_commit($this-&gt;link);
return 1;
}
}
return -1;
}
};
?&gt;</pre>
<div id="bsap_1266918" class="bsarocks bsap_af25dfd2f1908889af7a1aa5f4dcbd9e"></div><div style="clear:both;"></div>
]]></content:encoded>
			<wfw:commentRss>http://tympanus.net/codrops/2009/09/01/using-mysql-transactions-with-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  tympanus.net/codrops/tag/rollback/feed/ ) in 0.19248 seconds, on Feb 8th, 2012 at 5:20 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on Feb 8th, 2012 at 6:20 pm UTC -->
