2013-03-02 114 views
3

我無法連接到我的數據庫駐留在dotCloud。我想:如何使用php(託管在dotCloud上)連接到遠程mysql數據庫

$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name); 

$mysqli = mysqli_connect($db_host, $db_user, $db_password, $db_name); 

$mysqli = new mysqli($remote_server, $db_user, $db_password, $db_name); 

$mysqli = mysqli_connect($remote_server, $db_user, $db_password, $db_name); 

,但連接失敗,我也得到「錯誤324(淨值:: ERR_EMPTY_RESPONSE):服務器關閉沒有sendin的連接g任何數據。「

我找回動態上面下面的mysqli的腳本變量:

$env = json_decode(file_get_contents("/home/dotcloud/environment.json")); 
$db_user = $env->DOTCLOUD_DB_MYSQL_LOGIN; 
$db_password = $env->DOTCLOUD_DB_MYSQL_PASSWORD; 
$db_host = $env->DOTCLOUD_DB_MYSQL_HOST; 
$db_port = $env->DOTCLOUD_DB_MYSQL_PORT; 
$remote_server = '$db_host:$db_port'; 
//I also define $db_name here 
$db_name = 'mydbname'; 

我也有mysqli的腳本下面的下面的代碼:

if (mysqli_connect_error()) { 
    die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error()); 
    $result["status"] = "failed"; 
    $result["message"] = "Failed to connect to database."; 
    echo json_encode($result); 
    exit; 
} else { 
    // Successfully connected! 
    $stmt = $mysqli->stmt_init(); 
    echo "<p>Successfully connected!!</p>"; 
} 

我在做什麼錯?

+0

您的$ db_name變量在哪裏? – Stepo 2013-03-02 01:07:09

+0

@Stepo,我編輯了上面的代碼以顯示我定義$ db_name的位置。 – Naomi 2013-03-02 03:26:47

回答

12

您的代碼中有幾個錯誤。

1.您$ remote_server的變量是使用單引號

$remote_server = '$db_host:$db_port'; 

這意味着$ remote_server的不會擴大$ DB_HOST和$ DB_PORT變量。你應該使用雙引號。如果你使用了這個變量,它就不適合你。

$remote_server = "$db_host:$db_port"; 

更多信息,請參見本頁面: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

2.您不使用連接時,MySQL的端口,這是需要在dotCloud因爲它不標準端口上運行mysql 3306

您的代碼:

$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name); 

正確的代碼,使用變量,你上面已經宣佈:

$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name, $db_port); 

更多信息可以在這裏找到:http://www.php.net/manual/en/mysqli.quickstart.connections.php

對於一個完整的例子,它看起來像這樣。

$env = json_decode(file_get_contents("/home/dotcloud/environment.json")); 
$db_user = $env->DOTCLOUD_DB_MYSQL_LOGIN; 
$db_password = $env->DOTCLOUD_DB_MYSQL_PASSWORD; 
$db_host = $env->DOTCLOUD_DB_MYSQL_HOST; 
$db_port = $env->DOTCLOUD_DB_MYSQL_PORT; 
//I also define $db_name here 
$db_name = 'mydbname'; 

$mysqli = new mysqli($db_host, $db_user, $db_password, $db_name, $db_port); 
if ($mysqli->connect_errno) { 
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
} 

echo $mysqli->host_info . "\n"; 
+0

完美。我需要正確的語法。非常感謝你。 – Naomi 2013-03-04 23:49:19

+0

不客氣。如果您覺得它對您有幫助,請隨時投票給我的答案。 – 2013-03-05 11:35:09

+0

啊,是的,當然。我還是比較新的使用Stackoverflow。我點擊了複選標記,但沒有意識到我也可以點擊向上箭頭。 :) – Naomi 2013-03-05 22:22:40

-1
try like this: I think it will help you. 

$connect = mysql_connect($mysql_host, $mysql_user, $mysql_password); 
if (!$connect) { 
    die('Could not connect: ' . mysql_error()); 
} 
//echo 'Connected successfully'; 

$db = mysql_select_db($mysql_database,$connect); 

if (!$db) echo"'Could not select database"; 
+0

謝謝您的建議。我試過了,它給了我這個錯誤(和頁面沒有加載):錯誤324(net :: ERR_EMPTY_RESPONSE):服務器關閉連接而不發送任何數據。 – Naomi 2013-03-02 01:37:18

+0

Outch! **請不要舉例使用mysql_ *。此擴展從PHP 5.5.0開始已棄用。** – 2013-03-02 01:59:07

+0

您是否可以使用MySQL客戶端(SQLYog,MySQL Workbench等)連接到MySQL主機? – juanchopx2 2013-03-02 02:56:25

相關問題