Created
February 19, 2013 07:29
-
-
Save coreymcmahon/4983788 to your computer and use it in GitHub Desktop.
Using environment variables. From the article: The 12 Factor PHP App - Part 1, http://www.modernphpbook.com/articles/the-12-factor-php-app-part-1 - Fig 3
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 | |
// get the environment variable and parse it: | |
$url = parse_url(getenv('DATABASE_URL')); | |
// construct the data source name: | |
$scheme = $url['scheme']; | |
$host = $url['host'] . ':' . $url['port']; | |
$dbname = trim($url['path'], '/'); // '/application_db' -> 'application_db' | |
$dsn = "{$scheme}:host={$host};dbname={$path}"; | |
// connect to the database: | |
$pdo = new \PDO($dsn, $url['user'], $url['pass']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment