Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用MySQLi连接到多个数据库_Php_Mysqli - Fatal编程技术网

Php 使用MySQLi连接到多个数据库

Php 使用MySQLi连接到多个数据库,php,mysqli,Php,Mysqli,我需要使用PHP连接到两个数据库,并使用第一个查询的结果从第二个数据库中获取所需的其余数据 所以对于第二个连接,我需要连接到第二个数据库并选择state和zipcode,其中连接1(客户机)的结果等于数据库2中的firstname。我该怎么做 <?php // check if the 'id' variable is set in URL, and check that it is valid if (isset($_GET['cd']) && is_numeric($_

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

所以对于第二个连接,我需要连接到第二个数据库并选择state和zipcode,其中连接1(客户机)的结果等于数据库2中的firstname。我该怎么做

<?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

$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;
}

?>

您需要建立两个不同的连接

<?php
    $mysqliDB1 = new mysqli('localhost', 'DB1UserId', 'pwd', 'db1');

    $mysqliDB2 = new mysqli('localhost', 'DB2UserId', 'pwd', 'db2');

我猜的是表名等等,但我不是clarevoyant,所以你必须自己填写。

如果你想同时在两个数据库中执行查询,你需要有两个单独的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;

您需要创建两个不同的连接,如果您向我们展示数据库连接的PHP,我们可以help@RiggsFolly使用连接代码发布更新。这基本上是正常使用的MySQLi代码。代码的第一部分工作正常,但第二部分我似乎对Where语句有问题,并出现错误致命错误:对非对象调用成员函数fetch_object()。问题是在数据库1中,两个值位于一个名为client的列中,可能包含一个空格,而在第二个数据库中,有两个单独的列firstname和lastname。我尝试了这个
方法,其中CONCAT(firstname',lastname)={$client->firstname}
但仍然得到相同的错误
// 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;