<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[FrontAccounting forum — MySQL  OR  MySQLi Transaction]]></title>
		<link>https://frontaccounting.com/punbb/viewtopic.php?id=4726</link>
		<atom:link href="https://frontaccounting.com/punbb/extern.php?action=feed&amp;tid=4726&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in MySQL  OR  MySQLi Transaction.]]></description>
		<lastBuildDate>Thu, 27 Feb 2014 02:02:33 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=19342#p19342</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (apmuthu)]]></author>
			<pubDate>Thu, 27 Feb 2014 02:02:33 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=19342#p19342</guid>
		</item>
		<item>
			<title><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=19276#p19276</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (tester1)]]></author>
			<pubDate>Fri, 14 Feb 2014 06:52:40 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=19276#p19276</guid>
		</item>
		<item>
			<title><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=19275#p19275</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (tester1)]]></author>
			<pubDate>Fri, 14 Feb 2014 05:57:55 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=19275#p19275</guid>
		</item>
		<item>
			<title><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=19270#p19270</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (apmuthu)]]></author>
			<pubDate>Thu, 13 Feb 2014 14:34:57 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=19270#p19270</guid>
		</item>
		<item>
			<title><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=19269#p19269</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (tester1)]]></author>
			<pubDate>Thu, 13 Feb 2014 07:50:48 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=19269#p19269</guid>
		</item>
		<item>
			<title><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=19244#p19244</link>
			<description><![CDATA[<p>Let me try this one also</p>]]></description>
			<author><![CDATA[null@example.com (tester1)]]></author>
			<pubDate>Wed, 12 Feb 2014 04:39:55 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=19244#p19244</guid>
		</item>
		<item>
			<title><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=19238#p19238</link>
			<description><![CDATA[<p>If the UPDATE returns 0 (zero) rows affected, it may evaluate to false.</p>]]></description>
			<author><![CDATA[null@example.com (apmuthu)]]></author>
			<pubDate>Wed, 12 Feb 2014 04:26:02 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=19238#p19238</guid>
		</item>
		<item>
			<title><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=19222#p19222</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (ostertagi)]]></author>
			<pubDate>Fri, 07 Feb 2014 09:48:08 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=19222#p19222</guid>
		</item>
		<item>
			<title><![CDATA[Re: MySQL  OR  MySQLi Transaction]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=19215#p19215</link>
			<description><![CDATA[<p>at-least admin should reply on this ...</p>]]></description>
			<author><![CDATA[null@example.com (tester1)]]></author>
			<pubDate>Fri, 07 Feb 2014 04:45:39 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=19215#p19215</guid>
		</item>
		<item>
			<title><![CDATA[MySQL  OR  MySQLi Transaction]]></title>
			<link>https://frontaccounting.com/punbb/viewtopic.php?pid=19210#p19210</link>
			<description><![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>]]></description>
			<author><![CDATA[null@example.com (tester1)]]></author>
			<pubDate>Thu, 06 Feb 2014 09:18:28 +0000</pubDate>
			<guid>https://frontaccounting.com/punbb/viewtopic.php?pid=19210#p19210</guid>
		</item>
	</channel>
</rss>
