2011-12-20 107 views
0

所以我需要知道的是,我怎樣才能一次連接兩個數據庫。 我正在做的是從一個數據庫表中獲取一些信息並將其傳送到另一個數據庫。OOPHP連接兩個數據庫

我試過以下。

$db1 = mysql_connect(...); 
$db2 = mysql_connect(...); 

mysql_query('SELECT ...', $db1); 
mysql_query('INSERT ...', $db2); 

看的文檔http://www.php.net/mysql_connect和:

public function sitelistingcron() 
{ 

    $sqlk = mysql_query("SELECT * FROM customer_detail WHERE approvedforsite = 2 OR approvedforsite = 3"); 


    include_once('database2.php'); 
    $fet = new Dbase(); 
     $fet->Connect(); 
     $fet->DB(); 
    while($row = mysql_fetch_array($sqlk)){ 
     //ADD 
     $this->customer_id = $row['customer_id']; 
     $this->tradingname = $row['TradingName']; 
     $this->phone  = $row['Phone']; 
     $this->street  = $row['Street']; 
     $this->city   = $row['City']; 
     $this->state  = $row['State']; 
     $this->postcode  = $row['PostCode']; 
     $this->approved = $row['approvedforsite']; 




     $this->description = $row['Description']; 

     if($this->approved = 2) 
     { 
      $sqk = mysql_query("INSERT INTO approved_business_info (id, tradingname, phonenumber, street, postcode, suburb, discription) VALUES ({$this->customer_id}, '{$this->tradingname}', '{$this->phone}', '{$this->street}', '{$this->city}', '{$this->postcode}', '{$this->description}') ON DUPLICATE KEY UPDATE id = {$this->customer_id}, tradingname ='{$this->tradingname}', phonenumber ='{$this->phone}', street = '{$this->street}', postcode = '{$this->postcode}', suburb = '{$this->city}', discription = '{$this->discription}'") or mysql_error(); 


     print "INSERT INTO approved_business_info (id, tradingname, phonenumber, street, postcode, suburb, discription) VALUES ({$this->customer_id}, '{$this->tradingname}', '{$this->phone}', '{$this->street}', '{$this->city}', '{$this->postcode}', '{$this->description}') ON DUPLICATE KEY UPDATE id = {$this->customer_id}, tradingname ='{$this->tradingname}', phonenumber ='{$this->phone}', street = '{$this->street}', postcode = '{$this->postcode}', suburb = '{$this->city}', discription = '{$this->discription}'"; 


     } 

     //REMOVE 
     if($this->approved = 3) 
     { 
      $sqk = mysql_query("DELETE FROM `approved_business_info` WHERE id = {$this->customer_id}"); 
     } 
    } 
} 
+0

'$ result = sql_query($ conn1,$ sql1); $ sql2 = makeInsertStatements($ result); sql_update($ conn2,$ sql2);' – bdares 2011-12-20 01:54:18

+1

*你是如何連接到數據庫的?只要這樣做兩次...? – deceze 2011-12-20 01:56:45

+1

請停止使用古老的'mysql_ *'函數,並學習使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/mysqli)以及準備好的語句。 – 2011-12-20 05:21:02

回答

2

您可以通過簡單地跟蹤連接資源,並把它傳遞給每一個mysql_函數調用中使用使用mysql_擴展兩個單獨的DB連接其他mysql_功能。

+0

是的你可以這樣做,但我想出了另一種方式。 – RussellHarrower 2011-12-20 05:00:59

+0

小心分享您的解決方案? – deceze 2011-12-20 05:05:09