How to insert the last id from a table into another table in MySQL?

Posted by James on Stack Overflow See other posts from Stack Overflow or by James
Published on 2010-03-27T06:57:21Z Indexed on 2010/03/27 7:03 UTC
Read the original article Hit count: 179

Filed under:
|

I want to take the id of the most recent item in a database, increment it by one and insert that new id in to another table.

I tried:

$select = mysql_query("SELECT id FROM tableName ORDER BY id DESC");

while ($return = mysql_fetch_assoc($select)) {

   $id = $return['id'];
   $newId = $id++;

}

mysql_query("INSERT INTO anotherTable (someColumn) VALUES ('$newId')");

But it didn't work. How can this be done?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about php