在pHP中使用MySQLi连接到多个数据库

时间:2015-08-05 01:54:26

标签: php mysqli

我目前需要使用PHP连接到两个数据库,并使用第一个查询的结果从第二个数据库中获取我需要的其余数据。

因此,对于第二个连接,我需要连接到第二个数据库并选择状态和zipcode,其中连接1(客户端)的结果等于数据库2中的第一个名称。我将如何执行此操作?

<?php
     // check if the 'id' variable is set in URL, and check that it is valid
     if (isset($_GET['cd']) && is_numeric($_GET['cd']))

     // get id value
     $id = intval($_GET['cd']);

    $results = $id;
//Open a new connection to the MySQL server
require "calendarconnect.php";

//chained PHP functions
$client = $mysqli->query("SELECT client FROM appointments WHERE ID = $results")->fetch_object()->client; 
print  $client; //output value

$mysqli->close();
?>

连接到数据库代码类似于下面的

<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','some database','some password','some username');

//Output any connection error
if ($mysqli->connect_error) {
    die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}
?>

3 个答案:

答案 0 :(得分:2)

这未经过测试,但我认为它会像这样。

<?php

$dbc1 = new MySQLi()or die('error connecting to database');
$dbc2 = new MySQLi()or die('error connecting to database');



//build query 1
$query1 = "SELECT * FROM Table";

$result1 = $dbc1->query($query) or die("Error in query");
$thing1 = '';
// check result
if($result1->num_rows){
    //fetch result as object
    $row = $result1->fetch_object();

    //set attributes
    $thing1 = $row->Name;
}   


//build query 2
$query2 = "SELECT * FROM AnotherTable WHERE Id = '$thing1'";

$result2 = $dbc2->query($query) or die("Error in query");
$thing2 = '';
// check result
if($result2->num_rows){
    //fetch result as object
    $row = $result2->fetch_object();

    //set attributes
    $thing2 = $row->Name;
}

?>

答案 1 :(得分:1)

您需要进行2种不同的连接

CMake Error: -D must be followed with VAR=VALUE.
CMake Error: Problem processing arguments. Aborting.

现在当你使用<?php $mysqliDB1 = new mysqli('localhost', 'DB1UserId', 'pwd', 'db1'); $mysqliDB2 = new mysqli('localhost', 'DB2UserId', 'pwd', 'db2'); 时,你正在与DB1数据库交谈,当你使用$mysqliDB1->....时,你正在与DB2数据库交谈

所以

$mysqliDB2->....

我猜表格等等,但我不是克拉维斯特,所以你必须为自己填写。

答案 2 :(得分:0)

如果要在两个数据库中同时执行查询,则需要具有两个单独的mysqli对象。要打开连接,您可以使用以下代码:

// Don't forget to enable error reporting!
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$db1 = new mysqli('localhost', 'user', 'pass', 'dbName');
$db1->set_charset('utf8mb4'); // always set the charset

$db2 = new mysqli('localhost', 'user', 'pass', 'dbName2');
$db2->set_charset('utf8mb4'); // always set the charset

然后,您可以在每个数据库中分别执行两个语句。

// get id value
$id = intval($_GET['cd']);

// Get client name from DB1
$stmt = $db1->prepare('SELECT client FROM appointments WHERE ID = ?');
$stmt->bind_param('s', $id);
$stmt->execute();
$client = $stmt->get_result()->fetch_object();

// Get state and zipcode from DB2
$stmt = $db2->prepare('SELECT state,zipcode FROM location WHERE ClientName  = ?');
$stmt->bind_param('s', $client->client);
$stmt->execute();
$location = $stmt->get_result()->fetch_object();

echo $location->state;
echo $location->zipcode;