Last active
January 22, 2018 14:25
-
-
Save kaushalp/7a9249780f8d65fca7646edd02c7343b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample PHP code to read the connection string from application settings in Azure Web Apps