title: How does getting mysql's last insert ID work with transactions? + transaction questions
body: <p>A two part question:</p>
  
  <ol>
  <li><p>In my CodeIgniter script, I'm starting a transaction, then inserting a row, setting the insert_id() to a php variable, inserting more rows into another table using the new ID as a foreign key, and then I commit everything.</p>
  
  <p>So my question is: if everything does not commit before ending the transaction, how is mysql able to return the last insert ID, if nothing was even inserted? My script works (almost) perfectly, with the new ID being used in subsequent queries.</p>
  
  <p>(I say "almost" because, using the PDO mysql driver, sometimes the first insert that is supposed to return the insert_id() is duplicated--it get's inserted twice. Any idea why that would be? Is that related to getting the last ID? It never happens if using the mysqli or mysql driver.)</p></li>
  <li><p>I first wrote the script without transactions, so I have code that checks for mysql errors along the way, such as:</p>
  
  <pre><code>if(!$this-&gt;db-&gt;insert($table, $data)) {
      //log message here
  }
  </code></pre>
  
  <p>How does this affect the mysql process once I wrapped all my mysql code in a transaction? It's not causing any visible errors (hopefully unrelated to the problem stated above), but should it be removed?</p></li>
  </ol>
  
  <p>Thank you.</p>