1

I have deployed a php website to azure. It has a connection string like this:

    $host = 'localhost';
$username = 'xxxxx';
$password = 'xxxxx';
$database = 'xxxxx';


$con = mysql_connect($host, $username, $password);

I need to change this when it is deployed so it points to the ClearDB Mysql database. I have found the endpoint details for that are as follows:

Database=XXXX;Data Source=eu-cdbr-azure-north-c.XXXX.net;User Id=XXXX;Password=XXXX

This article here describes how to create a connection string using Environment Variables.

[http://azure.microsoft.com/blog/2013/07/17/windows-azure-web-sites-how-application-strings-and-connection-strings-work/]

But I am not sure how to make this work with a mysql_connect() function. I have tried substituting the values for host, username, password, and db with the details provided by Microsoft for the cleardb database but I receive an error 'An attempt was made to access a socket in a way forbidden by its access permissions '

Many thanks in advance for any suggestions.

Linda Keating
  • 2,215
  • 7
  • 31
  • 63

1 Answers1

1

In your php code you can do this

foreach ($_SERVER as $key => $value) {
    if (strpos($key, "MYSQLCONNSTR_") !== 0) {
        continue;
    }

    $host = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
    $username = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
    $password = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
    break;
}

/* now you can use the $host, $username, $password like you normally would */
$con = mysql_connect($host, $username, $password);
ahmelsayed
  • 7,125
  • 3
  • 28
  • 40