【问题标题】:Multiple connection database with mysqli使用 mysqli 的多连接数据库
【发布时间】:2019-01-29 04:18:16
【问题描述】:

我有 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;
    }

【问题讨论】:

  • $this-&gt;con for local 每次都会被覆盖。它将如何决定连接哪个数据库。传递一些标识符来决定connect()
  • 那么,我该怎么办?用变量替换它?

标签: php mysqli


【解决方案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;
    }
}

【讨论】:

  • $this-&gt;con 未定义。应该是 con_db1con_db2 取决于 OP 想要什么。
  • 我做了一些改动。它返回一个连接数组。
  • 我已经更新了问题,其中包含了执行查询
  • 但是我按照你之前说的修改后发现了一个错误,这个错误:Uncaught Error: Call to a member function prepare() on array
  • 我已经更新了我的答案。你可以试试看吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
  • 2018-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-22
  • 1970-01-01
相关资源
最近更新 更多