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

Naomi picture Naomi · Mar 2, 2013 · Viewed 21.7k times · Source

I am unable to connect to my database residing on 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."

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

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

What am I doing wrong?

Answer

Ken Cochrane picture Ken Cochrane · Mar 4, 2013

There are a couple of things wrong in your code.

1. Your $remote_server variable is using a single quote

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

See this page for more info: 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.

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

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