使用函数连接到远程mysql服务器

时间:2012-10-23 09:01:52

标签: php mysql

我正在使用wordpress,我有一个远程mysql服务器,用于未存储在wordpress db上的其他一些数据。

现在我想以php函数的形式连接到那个远程mysql服务器,但我不知道如何或是否可能。基本上,此函数用于检查远程mysql服务器上是否存在$orderid

function check_orderid($orderid) {
// Connect to database
// Check if $orderid exist if yes return true.
}

远程服务器确实接受远程连接。

此外,它是否可以,不会影响wordpress数据库连接?

2 个答案:

答案 0 :(得分:2)

除主机地址外,连接到远程服务器与连接本地服务器没有什么不同。只需用可解析的DNS名称或远程服务器的IP地址替换'localhost',你应该没问题。

答案 1 :(得分:0)

要进行多重连接,您必须使用链接。

它们是在成功连接后创建的:

$link1 = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
$link2 = mysqli_connect('localhost', 'my_user2', 'my_password2', 'my_db2');

您可以像以下一样使用它们:

mysqli_query($link1, "your first query as string");
mysqli_query($link2, "your second query as string");

通过官方OO版本,您应该使用:

$mysqli1 = new mysqli('localhost', 'my_user', 'my_password', 'my_db');
$mysqli2 = new mysqli('externalIP', 'my_user2', 'my_password2', 'my_db2');
$mysqli1->query("some query");
$mysqli1->query("some other query");

所以,在这种情况下,您可以轻松地执行此操作:

 $externaldbcon = new mysqli('ExtrnalDBIP', 'my_user', 'my_password', 'my_db');
 $externaldbcon->query("your query for external DB");