title: getting mysql_insert_id() while using ON DUPLICATE KEY UPDATE with PHP
body: <p>I've found a few answers for this using mySQL alone, but I was hoping someone could show me a way to get the ID of the last inserted or updated row of a mysql DB when using PHP to handle the inserts/updates.</p>
  
  <p>Currently I have something like this, where column3 is a unique key, and there's also an id column that's an autoincremented primary key:</p>
  
  <pre><code>$query ="INSERT INTO TABLE (column1, column2, column3) VALUES (value1, value2, value3) ON DUPLICATE KEY UPDATE SET column1=value1, column2=value2, column3=value3";
  mysql_query($query);
  
  $my_id = mysql_insert_id();
  </code></pre>
  
  <p>$my_id is correct on INSERT, but incorrect when it's updating a row (ON DUPLICATE KEY UPDATE).</p>
  
  <p>I have seen several posts with people advising that you use something like</p>
  
  <pre><code>INSERT INTO table (a) VALUES (0) ON DUPLICATE KEY UPDATE id=LAST_INSERT_ID(id) 
  </code></pre>
  
  <p>to get a valid ID value when the ON DUPLICATE KEY is invoked-- but will this return that valid ID to the PHP <code>mysql_insert_id()</code> function?</p>