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.

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.