views:

21

answers:

1

I have 2 database connections, and I want to get the last inserted ID from one of the connections.

$old_database = mysql_connect('host', 'username', 'password');
mysql_select_db('database1', $old_database);

$new_database = mysql_connect('host', 'username', 'password',true);
mysql_select_db('database2', $new_database);

$sql=mysql_query("INSERT INTO `table1`",$new_database);
$newid = mysql_insert_id();

Do I need to specify anything in the mysql_insert_id() function? I've been running into retrieving the last known ID, and I think it's due to this.

+3  A: 

Yes you need to specify the MySQL Resource Link Identifier, see: http://us2.php.net/manual/en/function.mysql-insert-id.php

Like this:

$sql = mysql_query("INSERT INTO `table1`",$new_database);
$newid = mysql_insert_id($new_database);
jordanstephens