title: how do I give out incrementing keys to client side from php and mysql ?
body: <p>I have a simple table </p>
  
  <pre><code>CREATE TABLE `keys` (
  `IDkey` int NOT NULL auto_increment,
  `username` varchar(50) NOT NULL, 
  PRIMARY KEY  (`IDkey`)
  ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
  </code></pre>
  
  <p>and give out incrementing integer keys 'IDkey' to users as they call in with my webapp. I also store their username in the table.  </p>
  
  <p>I have two questions I'm stuck on.</p>
  
  <p>First question: at the moment I am doing the following to give out the key, is there a way to combine this into one query?</p>
  
  <pre><code>INSERT INTO keys VALUES (NULL, '$username');
  SELECT IDkey FROM keys WHERE username='$username';
  </code></pre>
  
  <p>Second Question: the key expires so the same user may return for a new key but this causes a problem because their username is already in the database.  Is there a way to write my SELECT query so it returns the most recent record by that user?</p>