php - OOPHP 连接两个数据库

标签 php mysql sql database

我需要知道的是如何同时连接两个数据库。 我正在做的是从一个数据库表中获取一些信息并将其传输到另一个数据库。

我已经尝试过以下方法。

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

最佳答案

您可以使用 mysql_ 扩展来使用两个单独的数据库连接,只需跟踪连接资源并将其传递给每个 mysql_ 函数调用即可:

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

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

查看 http://www.php.net/mysql_connect 的文档以及其他 mysql_ 函数。

关于php - OOPHP 连接两个数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8569904/

相关文章:

php - 显示URL内容

PHP-警告 : array_map(): Argument #2 should be an array

c# - 为 Localhost c# 使用 Oledb 连接字符串

php数组通过变量数组获取多维值

Java BlackBerry - 如何使用 GET 方法正确调用 php 脚本?

PHP 随机查询

mysql - 缓慢的mysql子查询

php - Zend Framework 1 获取没有重复的结果

mysql - 如何将数据库记录存储到哈希 ruby​​ on rails

php - php根据id添加数据库字段