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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15169458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:42:04  来源:igfitidea点击:

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

phpmysqliremote-serverdotcloud

提问by Naomi

I am unable to connect to my database residing on dotCloud. I tried:

我无法连接到驻留在 dotCloud 上的数据库。我试过:

$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?

我究竟做错了什么?

回答by Ken Cochrane

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. 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 变量。你应该使用双引号。如果您按原样使用该变量,它将对您不起作用。

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

回答by Semantic web

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