简体   繁体   English

PHP,MySQL-多个数据库连接和mysql_insert_id()

[英]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. 我有2个数据库连接,我想从其中一个连接中获取最后插入的ID。

$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? 我需要在mysql_insert_id()函数中指定任何内容吗? I've been running into retrieving the last known ID, and I think it's due to this. 我一直在尝试检索最后一个已知的ID,我认为是由于这个原因。

Yes you need to specify the MySQL Resource Link Identifier, see: http://us2.php.net/manual/en/function.mysql-insert-id.php 是的,您需要指定MySQL资源链接标识符,请参见: http : //us2.php.net/manual/zh/function.mysql-insert-id.php

Like this: 像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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