【发布时间】:2015-08-05 01:54:26
【问题描述】:
我需要使用 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);
}
【问题讨论】:
-
你需要创建 2 个不同的连接,所以如果你向我们展示你的 PHP 用于数据库连接,我们可以提供帮助
-
@RiggsFolly 使用连接代码发布更新。它基本上是使用的普通 MySQLi 代码。