[英]Multiple connection database with 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;
}
您可以進行以下更改嗎? 在這里,我使用兩個局部變量。 $ 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.