我需要使用PHP连接两个数据库,并使用第一个查询的结果从第二个数据库中获取其余所需数据。
因此,对于第二个连接,我需要连接到第二个数据库并选择状态和邮政编码,其中连接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);
}