title: How does mysqli::commit & mysqli::rollback work?
body: <p>I've read the online php manual but I'm still not sure of the way these two functions work: mysqli::commit &amp; mysqli::rollback.</p>
  
  <p>The first thing I have to do is to:</p>
  
  <pre><code>$mysqli-&gt;autocommit(FALSE);
  </code></pre>
  
  <p>Then I make some queries:</p>
  
  <pre><code>$mysqli-&gt;query("...");
  $mysqli-&gt;query("...");
  $mysqli-&gt;query("...");
  </code></pre>
  
  <p>Then I commit the transaction consisting of these 3 queries by doing:</p>
  
  <pre><code>$mysqli-&gt;commit();
  </code></pre>
  
  <p>BUT in the unfortunate case in which one of these queries does not work, do all 3 queries get cancelled or do I have to call a rollback myself? I want all 3 queries to be atomic and be considered as only one query. If one query fails then all 3 should fail and have no effect.</p>
  
  <p>I'm asking this because in the comments I've seen on the manual page: <a href="http://php.net/manual/en/mysqli.commit.php" rel="noreferrer">http://php.net/manual/en/mysqli.commit.php</a>
  the user Lorenzo calls a rollback if one of the queries failed.</p>
  
  <p>What's a rollback good for if the 3 queries are atomic? I don't understand.</p>
  
  <p>EDIT: This is the code example I am doubtful about:</p>
  
  <pre><code>&lt;?php 
  $all_query_ok=true; // our control variable 
  $mysqli-&gt;autocommit(false);
  //we make 4 inserts, the last one generates an error 
  //if at least one query returns an error we change our control variable 
  $mysqli-&gt;query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false; 
  $mysqli-&gt;query("INSERT INTO myCity (id) VALUES (200)") ? null : $all_query_ok=false; 
  $mysqli-&gt;query("INSERT INTO myCity (id) VALUES (300)") ? null : $all_query_ok=false; 
  $mysqli-&gt;query("INSERT INTO myCity (id) VALUES (100)") ? null : $all_query_ok=false; //duplicated PRIMARY KEY VALUE 
  
  //now let's test our control variable 
  $all_query_ok ? $mysqli-&gt;commit() : $mysqli-&gt;rollback(); 
  
  $mysqli-&gt;close(); 
  ?&gt;
  </code></pre>
  
  <p>I think this code is wrong because if any of the queries failed and <code>$all_query_ok==false</code> then you don't need to do a rollback because the transaction was not processed. Am I right?</p>