我需要连接来自不同数据库的两个表。
代码看起来像这样:
$tbl1 = 'table1';
$tbl2 = 'table2';
$DB1 = 'DB1';
$DB2 = 'DB2';
$connect1 = mysql_connect(DB_SERVER,DB_USER1,DB_PASSWORD1);
mysql_select_db ($DB1, $connect1);
$connect2 = mysql_connect(DB_SERVER,DB_USER2,DB_PASSWORD2);
mysql_select_db ($DB2, $connect2);
/* note both the DB are in the same server and users has privileges to theyr own datadases say usr1 to DB1, usr2 to DB2 */
mysql_query("SELECT * FROM $DB1.$tbl1 AS tb1 LEFT JOIN $DB2.$tbl2 AS tb2 ON tb1.id = tb2.id", $connect1);
上面的查询返回一个错误,说 table2 不存在,很明显因为我正在使用$connect1
帮助我的人!