简体   繁体   English

如何使用php(托管在dotCloud上)连接到远程mysql数据库

[英]How to connect to remote mysql database using php (hosted on dotCloud)

I am unable to connect to my database residing on dotCloud. 我无法连接到驻留在dotCloud上的数据库。 I tried: 我试过了:

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

and

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

and

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

and

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

but it fails to connect, and I get "Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data." 但它无法连接,我得到“错误324(net :: ERR_EMPTY_RESPONSE):服务器关闭连接而不发送任何数据。”

I retrieve variables dynamically above the mysqli script with the following: 我使用以下代码在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';

I also have the following code below the mysqli script: 我在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>";
}

What am I doing wrong? 我究竟做错了什么?

There are a couple of things wrong in your code. 您的代码中存在一些错误。

1. Your $remote_server variable is using a single quote 1.您的$ remote_server变量使用单引号

$remote_server = '$db_host:$db_port';

This means that $remote_server will not expand the $db_host and $db_port variables. 这意味着$ remote_server不会扩展$ db_host和$ db_port变量。 You should use double quotes. 你应该使用双引号。 If you used the variable as it is, it wouldn't work for you. 如果您按原样使用变量,它将不适合您。

$remote_server = "$db_host:$db_port";

See this page for more info: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single 有关详细信息,请参阅此页面: http//www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

2. You are not using the mysql port when connecting, which is required on dotCloud since it doesn't run mysql on the standard port of 3306. 2.连接时没有使用mysql端口,这在dotCloud上是必需的,因为它不会在标准端口3306上运行mysql。

Your code: 你的代码:

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

The correct code, using the variables you already declared above: 正确的代码,使用您已在上面声明的变量:

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

More info can be found here: http://www.php.net/manual/en/mysqli.quickstart.connections.php 更多信息可以在这里找到: http//www.php.net/manual/en/mysqli.quickstart.connections.php

For a complete example, it will look like this. 有关完整示例,它将如下所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM