Skip to content

Instantly share code, notes, and snippets.

@tonyclemmey
Forked from coreymcmahon/db-env-var.php
Created July 29, 2020 10:33
Show Gist options
  • Save tonyclemmey/c7a63a2777dc799803f326a332b96835 to your computer and use it in GitHub Desktop.
Save tonyclemmey/c7a63a2777dc799803f326a332b96835 to your computer and use it in GitHub Desktop.
Initialising a DB connection from 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 1
<?php
// Initialising a DB connection from environment vars...
$dbHost = getenv('DB_HOST');
$dbName = getenv('DB_NAME');
$dbUser = getenv('DB_USER');
$dbPass = getenv('DB_PASS');
$pdo = new \PDO("mysql:host={$dbHost};dbname={$dbName}", $dbUser, $dbPass);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment