如何使用php连接到远程mysql数据库(托管在dotCloud上) [英] How to connect to remote mysql database using php (hosted on dotCloud)

查看:135
本文介绍了如何使用php连接到远程mysql数据库(托管在dotCloud上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法连接到位于dotCloud上的数据库.我试过了:

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

$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(net :: ERR_EMPTY_RESPONSE):服务器关闭了连接,但未发送任何数据."

but it fails to connect, and I get "Error 324 (net::ERR_EMPTY_RESPONSE): The server closed the connection without sending any data."

我使用以下命令在mysqli脚本上方动态检索变量:

I retrieve variables dynamically above the mysqli script with the following:

$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脚本下面也有以下代码:

I also have the following code below the mysqli script:

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

我在做什么错了?

推荐答案

您的代码中有几处错误.

There are a couple of things wrong in your code.

1.您的$ remote_server变量使用单引号

$remote_server = '$db_host:$db_port';

这意味着$ 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";

有关更多信息,请参见此页面: http://www.php .net/manual/en/language.types.string.php#language.types.string.syntax.single

See this page for more info: http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.single

2.连接时您未使用mysql端口,这是dotCloud所必需的,因为它没有在3306的标准端口上运行mysql.

您的代码:

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

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

More info can be found here: 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";

这篇关于如何使用php连接到远程mysql数据库(托管在dotCloud上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆