Php 需要访问两个数据库
我有两个数据库相同的服务器相同的域和相同的用户名和密码。我需要从主数据库中选择,然后是第二个数据库,我需要能够连接到它并插入数据 这是我目前正在尝试的,但它不起作用Php 需要访问两个数据库,php,mysql,database,join,Php,Mysql,Database,Join,我有两个数据库相同的服务器相同的域和相同的用户名和密码。我需要从主数据库中选择,然后是第二个数据库,我需要能够连接到它并插入数据 这是我目前正在尝试的,但它不起作用 public function publish(){ $result = mysql_query("SELECT * FROM customer_detail WHERE approvedforsite = 2"); while($row = mysql_fetch_array($result))
public function publish(){
$result = mysql_query("SELECT * FROM customer_detail WHERE approvedforsite = 2");
while($row = mysql_fetch_array($result))
{
echo $row['customer_id'] . " - " .$row['TradingName'] . " - " . $row['Phone'] . " - " . $row['Street'] . " - " . $row['City'] . " - " . $row['State'] . " - " . $row['PostCode'] . " - " .$row['Description'];
echo "<br />";
mysql_query("INSERT INTO realcas_incard_server.approved_business (customer_id, tradingname)
VALUES ('". $row['customer_id'] ."','".$row['TradingName']."')");
mysql_query("INSERT INTO realcas_incard_server.business_stores (customer_id, storeid, phone, street, suburb, state, postcode, description)
VALUES ('". $row['customer_id'] ."', 1, '".$row['Phone']."', '" .$row['Street'] . "', '" . $row['City'] . "', '" . $row['State'] . "', '" . $row['PostCode'] . "','".$row['Description']."')");
$offerresult = mysql_query("SELECT * FROM customer_realcash_offer WHERE businessid = ".$row['customer_id']);
while($offers = mysql_fetch_array($offerresult))
{
mysql_query("INSERT INTO _incard_server.Real_Cash_Offers (business_id,storeid,offer) VALUES (".$row['customer_id'].",1,'".$offers['offer']."')");
echo $offers['offer']. "<br/>";
}
echo "<br />";
echo "<br />";
}
}
您可以使用mysql\u select\u db在同一服务器上的数据库之间切换/username/password/connection,也可以使用database\u name.table\u name.column\u name虚线语法,使别名更容易使用,如从database\u name.table\u name中选择a.column作为a
这是你的两个选择。第三个选项是只使用一个数据库,但我假设这不是一个选项,首先在数据库中有一个表测试
试验
id int
其次是数据库中的表垃圾
垃圾
id int
然后
问题是
插入junkid,从first.test中选择id 但它不起作用-不是一个错误描述。