<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
	<title type="html"><![CDATA[FrontAccounting forum — MySQL  OR  MySQLi Transaction]]></title>
	<link rel="self" href="https://frontaccounting.com/punbb/extern.php?action=feed&amp;tid=4726&amp;type=atom" />
	<updated>2014-02-27T02:02:33Z</updated>
	<generator>PunBB</generator>
	<id>https://frontaccounting.com/punbb/viewtopic.php?id=4726</id>
		<entry>
			<title type="html"><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=19342#p19342" />
			<content type="html"><![CDATA[<p>Please be clear on what checks you want before a commit is envisaged:</p><p>Each insert must be successful - define what you mean by successful:<br /> - whether the record was inserted or <br /> - whether it already exists and did not need the insert or <br /> - that the insert values were not acceptable for other reasons like field type and size checks.</p><p>Similarly about the definition of the UPDATE sql being successful:<br /> - was the update successful in general<br /> - was the update unnecessary as the record was already in the updated state<br /> - was the update not acceptable at all for reasons like field type and size check failure.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2014-02-27T02:02:33Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=19342#p19342</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=19276#p19276" />
			<content type="html"><![CDATA[<p>What say NOW :</p><br /><p>function autocommitfalse(){<br />&nbsp; &nbsp; &nbsp; global $db; <br />&nbsp; &nbsp; &nbsp; /* switch autocommit status to FALSE. Actually, it starts transaction */<br />&nbsp; &nbsp; &nbsp; return mysqli_autocommit($db, FALSE); <br />}</p><p>function autocommittrue(){<br />&nbsp; &nbsp; &nbsp; global $db; <br />&nbsp; &nbsp; &nbsp; return mysqli_autocommit($db, TRUE); <br />}</p><br /><p>function commitquery(){<br />&nbsp; &nbsp; &nbsp;global $db;<br />&nbsp; &nbsp; &nbsp;return mysqli_commit($db);<br />}</p><p>function rollbackedquery(){<br />&nbsp; &nbsp; &nbsp;global $db;<br />&nbsp; &nbsp; &nbsp;return mysqli_rollback($db);<br />}</p><br /><p>======</p><div class="codebox"><pre><code>try {
    
     autocommitfalse();

    $SQLSELECT = &quot;SELECT * FROM BC &quot;;
    $result_as = db_query($SQLSELECT);

    if(!$result_as ){
            throw new Exception(&#039;Wrong SQL SELECT: &#039; . $SQLSELECT_a. &#039; Error: &#039;.db_error_msg($db) . db_error_no());
     }
 

    $sqla =&quot;INSERT INTO A () VALUES () &quot;;
    $result_a = db_query($sqla);

    if(!$result_a){
            throw new Exception(&#039;Wrong SQL SELECT: &#039; . $result_a. &#039; Error: &#039;.db_error_msg($db) . db_error_no());
     }
 
   
    $sqlb =&quot;INSERT INTO B () VALUES () &quot;;
    $result_b =db_query($sqlb);
   
    if(!$result_b){
            throw new Exception(&#039;Wrong SQL SELECT: &#039; . $result_a. &#039; Error: &#039;.db_error_msg($db) . db_error_no());
     }
  
    $sqlCb =&quot;UPDATE CB ()  SET ...   &quot;;
    $result_Cb =db_query($sqlCb);

    if($result_Cb === false  &amp;&amp;  mysqli_affected_rows($db) == 0 ){
            throw new Exception(&#039;Wrong SQL SELECT: &#039; . $result_a. &#039; Error: &#039;.db_error_msg($db) . db_error_no());
     }
     

    commitquery();
    echo &#039;Transaction completed successfully!&#039;;

} catch (Exception $e) {

        echo&quot;&lt;br \&gt;&quot;;
        echo &quot;&lt;table align=center&gt;&lt;tr&gt;&lt;td&gt;&quot;;
        echo $e-&gt;getMessage();
        echo &quot;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&quot;;
        echo&quot;&lt;br \&gt;&quot;;

        echo display_error(_(&quot;Transaction failed: transaction rolled back&quot;));
        rollbackedquery();
   }        

        autocommittrue();</code></pre></div><br /><p>If something wrong ..please let me know with change in it ( above code ) ..so that i am more clear ....</p><p>Ohh i think still not rollback... hmm admin please help me guys please</p>]]></content>
			<author>
				<name><![CDATA[tester1]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=18290</uri>
			</author>
			<updated>2014-02-14T06:52:40Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=19276#p19276</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=19275#p19275" />
			<content type="html"><![CDATA[<p>@apmuthu</p><p>Can you modify above code and let me know what exact you mean to say , as i got to know that :</p><p>mysql_affected_rows() should be more useful to return true update and insert.</p><p>Please help me with above example in your way. so that i would be more clear what you want to say.</p>]]></content>
			<author>
				<name><![CDATA[tester1]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=18290</uri>
			</author>
			<updated>2014-02-14T05:57:55Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=19275#p19275</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=19270#p19270" />
			<content type="html"><![CDATA[<p>Check out these links to prove that the UPDATE query check is not functioning as needed:</p><p>http://stackoverflow.com/questions/14908087/should-mysqli-update-queries-return-a-result<br />http://stackoverflow.com/questions/12990164/how-to-check-if-an-update-mysqli-query-is-correctly-executed<br />http://stackoverflow.com/questions/12744953/mysqli-returns-true-for-insert-query-row-not-inserted</p><p>UPDATE will return 0 rows affected if the changed state already exists and the mysql_query function will still return true. Hence remove the UPDATE check in the if statement.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2014-02-13T14:34:57Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=19270#p19270</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=19269#p19269" />
			<content type="html"><![CDATA[<p>Below is what i have tried , please let me know which one is SAFE as i have many query and if any of my condition fails i need rollback + ERROR message ...</p><br /><div class="quotebox"><blockquote><p>try {<br />&nbsp; /* switch autocommit status to FALSE. Actually, it starts transaction */<br />&nbsp; global $db;<br />&nbsp; <br />&nbsp; mysqli_autocommit($db, FALSE);</p><p>&nbsp; &nbsp; $sqla =&quot;INSERT INTO A () VALUES () &quot;;<br />&nbsp; &nbsp; $result_a = db_query($sqla);<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; if($result_a){<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; $sqlb =&quot;INSERT INTO B () VALUES () &quot;;<br />&nbsp; &nbsp; $result_b =db_query($sqlb);<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; if($result_b){<br />&nbsp; &nbsp; $sqlCb =&quot;UPDATE CB ()&nbsp; SET ...&nbsp; &nbsp;&quot;;<br />&nbsp; &nbsp; $result_Cb =db_query($sqlCb);<br />&nbsp; &nbsp; }else { echo &quot;ERROR&quot; ; }<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; } else { echo &quot;ERROR&quot; ; }</p><p>&nbsp; mysqli_commit($db);<br />&nbsp; echo &#039;Transaction completed successfully!&#039;;</p><p>} catch (Exception $e) {</p><p>&nbsp; echo &#039;Transaction failed: &#039; . $e-&gt;display_notification_centered(_(&quot;transaction rolled back&quot;));<br />&nbsp; mysqli_rollback($db);<br />}</p><p>/* switch back autocommit status */<br />&nbsp; mysqli_autocommit($db, TRUE);</p><br /><br /><br /><p>//--------------------------------OR---------------------------------</p><br /><p>begin_transaction();</p><p>$sqla =&quot;INSERT INTO A () VALUES () &quot;;<br />$result_a = db_query($sqla);</p><p>$sqlb =&quot;INSERT INTO B () VALUES () &quot;;<br />$result_b =db_query($sqlb);</p><p>$sqlCb =&quot;UPDATE CB ()&nbsp; SET ...&nbsp; &nbsp;&quot;;<br />$result_Cb =db_query($sqlCb);</p><p>if ( $result_a &amp;&amp; $result_b &amp;&amp; $result_Cb ) {<br />&nbsp; &nbsp; commit_transaction();<br />&nbsp; &nbsp; echo display_notification_centered(_(&quot;Database transaction was successful.&quot;));<br />} else {&nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; cancel_transaction();<br />&nbsp; &nbsp; echo display_notification_centered(_(&quot;transaction rolled back&quot;));<br />&nbsp; &nbsp; exit;<br />}</p><br /><p>//--------------------------------OR---------------------------------</p><br /><br /><p>db_query(&quot;BEGIN&quot;);</p><p>$sqla =&quot;INSERT INTO A () VALUES () &quot;;<br />$result_a = db_query($sqla);</p><p>$sqlb =&quot;INSERT INTO B () VALUES () &quot;;<br />$result_b =db_query($sqlb);</p><p>$sqlCb =&quot;UPDATE CB ()&nbsp; SET ...&nbsp; &nbsp;&quot;;<br />$result_Cb =db_query($sqlCb);</p><p>if ( $result_a &amp;&amp; $result_b &amp;&amp; $result_Cb ) {<br />&nbsp; &nbsp; db_query(&quot;COMMIT&quot;);<br />&nbsp; &nbsp; echo display_notification_centered(_(&quot;Database transaction was successful.&quot;));<br />} else {&nbsp; &nbsp; &nbsp; &nbsp;<br />&nbsp; &nbsp; db_query(&quot;ROLLBACK&quot;);<br />&nbsp; &nbsp; echo display_notification_centered(_(&quot;transaction rolled back&quot;));<br />&nbsp; &nbsp; exit;<br />}</p></blockquote></div>]]></content>
			<author>
				<name><![CDATA[tester1]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=18290</uri>
			</author>
			<updated>2014-02-13T07:50:48Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=19269#p19269</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=19244#p19244" />
			<content type="html"><![CDATA[<p>Let me try this one also</p>]]></content>
			<author>
				<name><![CDATA[tester1]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=18290</uri>
			</author>
			<updated>2014-02-12T04:39:55Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=19244#p19244</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=19238#p19238" />
			<content type="html"><![CDATA[<p>If the UPDATE returns 0 (zero) rows affected, it may evaluate to false.</p>]]></content>
			<author>
				<name><![CDATA[apmuthu]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=364</uri>
			</author>
			<updated>2014-02-12T04:26:02Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=19238#p19238</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=19222#p19222" />
			<content type="html"><![CDATA[<p>Checking the db_query() function shows that its returns false on an error. So use the return status. It will return false if it fails, which you can log (or whatever you want to do) and then continue.</p><p>$result_a = db_query($sqla);<br />$result_b = db_query($sqlb);<br />$result_Cb = db_query($sqlCb);</p><p>if ( $result_a &amp;&amp; $result_b &amp;&amp; $result_Cb ) {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db_query(&quot;COMMIT&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo display_notification_centered(_(&quot;Database transaction was successful.&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db_query(&quot;ROLLBACK&quot;);<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;echo display_notification_centered(_(&quot;transaction rolled back&quot;));<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>]]></content>
			<author>
				<name><![CDATA[ostertagi]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=18743</uri>
			</author>
			<updated>2014-02-07T09:48:08Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=19222#p19222</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=19215#p19215" />
			<content type="html"><![CDATA[<p>at-least admin should reply on this ...</p>]]></content>
			<author>
				<name><![CDATA[tester1]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=18290</uri>
			</author>
			<updated>2014-02-07T04:45:39Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=19215#p19215</id>
		</entry>
		<entry>
			<title type="html"><![CDATA[MySQL  OR  MySQLi Transaction]]></title>
			<link rel="alternate" href="https://frontaccounting.com/punbb/viewtopic.php?pid=19210#p19210" />
			<content type="html"><![CDATA[<p>Can any one tell me </p><p>Try {} Catch {} Transaction for FA ?</p><p>I am using currently as :</p><div class="codebox"><pre><code>db_query(&quot;START TRANSACTION&quot;);

$sqla =&quot;INSERT INTO A () VALUES () &quot;;
db_query($sqla);

$sqlb =&quot;INSERT INTO B () VALUES () &quot;;
db_query($sqlb);

$sqlCb =&quot;UPDATE CB ()  SET ...   &quot;;
db_query($sqlCb);

if ( $sqla and $sqlb and $sqlCb ) {
                db_query(&quot;COMMIT&quot;);
                echo display_notification_centered(_(&quot;Database transaction was successful.&quot;));
            } else {        
                db_query(&quot;ROLLBACK&quot;);
               echo display_notification_centered(_(&quot;transaction rolled back&quot;));
                exit;
            }</code></pre></div><br /><p>I am Right ? ...</p><p>Can any one please tell me same in TRY .. CATCH ? ....</p>]]></content>
			<author>
				<name><![CDATA[tester1]]></name>
				<uri>https://frontaccounting.com/punbb/profile.php?id=18290</uri>
			</author>
			<updated>2014-02-06T09:18:28Z</updated>
			<id>https://frontaccounting.com/punbb/viewtopic.php?pid=19210#p19210</id>
		</entry>
</feed>
