簡體   English   中英

使用函數連接到遠程mysql服務器

[英]Connecting to a remote mysql server using a function

我正在使用wordpress,我有一個遠程mysql服務器,用於未存儲在wordpress db上的其他一些數據。

現在我想以php函數的形式連接到那個遠程mysql服務器,但我不知道如何或是否可能。 基本上這個函數用於檢查遠程mysql服務器上是否存在$orderid

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

遠程服務器確實接受遠程連接。

此外,它是否可以,並不會影響wordpress數據庫連接?

除主機地址外,連接到遠程服務器與連接本地服務器沒有什么不同。 只需用可解析的DNS名稱或遠程服務器的IP地址替換'localhost',你應該沒問題。

要建立多個連接,您必須使用鏈接。

它們是在成功連接后創建的:

$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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM