Skip to content

Instantly share code, notes, and snippets.

@kaushalp
Last active January 22, 2018 14:25
Show Gist options
  • Save kaushalp/7a9249780f8d65fca7646edd02c7343b to your computer and use it in GitHub Desktop.
Save kaushalp/7a9249780f8d65fca7646edd02c7343b to your computer and use it in GitHub Desktop.
<?php
$connstr = getenv("MYSQLCONNSTR_MySqlDB");
//Parse the above environment variable to retrieve username, password and hostname.
foreach ($_SERVER as $key => $value)
{
if (strpos($key, "MYSQLCONNSTR_") !== 0)
{
continue;
}
$hostname = preg_replace("/^.*Data Source=(.+?);.*$/", "\\1", $value);
$username = preg_replace("/^.*User Id=(.+?);.*$/", "\\1", $value);
$password = preg_replace("/^.*Password=(.+?)$/", "\\1", $value);
break;
}
echo "Server Name: ".$hostname."</br>";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) or die("Unable to connect to MySQL");
echo "<br>Connected to DB server successfully</br>";
//select a database to work with
$selectDb = mysql_select_db("db_name",$dbhandle) or die("Could not select database");
//execute the SQL query and return records
$sqlQuery = mysql_query("SELECT * FROM Table") or die("Could not query database");
mysql_close($dbhandle);
?>
@kaushalp
Copy link
Author

kaushalp commented May 4, 2017

Sample PHP code to read the connection string from application settings in Azure Web Apps

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment