简体   繁体   中英

Php, MySql - Multiple DB connections and mysql_insert_id()

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM