使用mysqli的多重连接数据库

时间:2019-01-29 04:18:16

标签: php mysqli

我有2个不同的服务器数据库,然后我想创建一个连接函数,以便可以使用连接表从不同的数据库检索数据。 以前我已经做了如下功能,只有那些连接到本地数据库的功能...

<?php
//Constants to connect with the database local
define('DB_USERNAME_LOCAL', 'root');
define('DB_PASSWORD_LOCAL', '');
define('DB_HOST_LOCAL', 'localhost');
define('DB_NAME_LOCAL', 'db_attendance');

//Constants to connect with the database server
define('DB_USERNAME_SERVER', '******');
define('DB_PASSWORD_SERVER', '******');
define('DB_HOST_SERVER', '*********');
define('DB_NAME_SERVER', 'rlempl');

class DbConnect
{
    //Variable to store database link
    private $con;
    //This method will connect to the database
    function connect()
    {
        //connecting to mysql database
        $this->con = new mysqli(DB_HOST_LOCAL, DB_USERNAME_LOCAL, DB_PASSWORD_LOCAL, DB_NAME_LOCAL);
        $this->con = new mysqli(DB_HOST_SERVER, DB_USERNAME_SERVER, DB_PASSWORD_SERVER, DB_NAME_SERVER);

        //Checking if any error occured while connecting
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        //finally returning the connection link
        return $this->con;
    }
}


// this is function for query
<?php

class DbOperation
{
    private $con;

    function __construct()
    {
        require_once dirname(__FILE__) . '/DbConnect.php';
        $db = new DbConnect();
        $this->con = $db->connect();
    }

// this query join 2 table with diferent database
   public function dataExist($pin, $date){
        $stmt = $this->con->prepare("SELECT b.rldate FROM tams_fingerid a JOIN tams_attlog b ON b.rlcode=a.rlcode WHERE a.fingerid='$pin' AND b.rldate='$date'");
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows > 0;
    }

1 个答案:

答案 0 :(得分:1)

您可以进行以下更改吗?在这里,我使用两个局部变量。 $ con_db1用于一个数据库,$ con_db2用于另一个数据库。

我已通过执行查询更改了代码

    <?php
//Constants to connect with the database local
define('DB_USERNAME_LOCAL', 'root');
define('DB_PASSWORD_LOCAL', '');
define('DB_HOST_LOCAL', 'localhost');
define('DB_NAME_LOCAL', 'db_attendance');

//Constants to connect with the database server
define('DB_USERNAME_SERVER', '******');
define('DB_PASSWORD_SERVER', '******');
define('DB_HOST_SERVER', '*********');
define('DB_NAME_SERVER', 'rlempl');

class DbConnect
{
    //Variable to store database link
    private $con_db1;
    private $con_db2;
    //This method will connect to the database
    function connect()
    {
        //connecting to mysql database
        $this->con_db1 = new mysqli(DB_HOST_LOCAL, DB_USERNAME_LOCAL, DB_PASSWORD_LOCAL, DB_NAME_LOCAL);
        $this->con_db2 = new mysqli(DB_HOST_SERVER, DB_USERNAME_SERVER, DB_PASSWORD_SERVER, DB_NAME_SERVER);

        //Checking if any error occured while connecting
        if (mysqli_connect_errno()) {
            echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        //finally returning the connection link
        return ['db1'=>$this->con_db1,'db2'=>$this->con_db2];
    }
}
?>


// this is function for query
<?php

class DbOperation
{
    private $db1;
    private $db2;

    function __construct()
    {
        require_once dirname(__FILE__) . '/DbConnect.php';
        $db = new DbConnect();
        $connections = $db->connect();
        $this->db1 = $connections['db1'];
    }

// this query join 2 table with diferent database
    public function dataExist($pin, $date)
    {
        $stmt = $this->db1->prepare("SELECT b.rldate FROM tams_fingerid a JOIN tams_attlog b ON b.rlcode=a.rlcode WHERE a.fingerid='$pin' AND b.rldate='$date'");
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows > 0;
    }
}