Wallogit.com
2017 © Pedro Peláez
Parses a database URL from the environment into a DSN connection string for use in PHP's PDO., (*1)
Useful for parsing heroku config DATABASE_URL variable., (*2)
e.g., (*3)
A database URL set in an environment variable:, (*4)
export DATABASE_URL=postgres://USER:PASS@HOST:PORT/DBNAME
...can be parsed into this string:, (*5)
'pgsql:host=HOST;port=PORT;user=USER;dbname=DBNAME;password=PASSWORD'
...using this code:, (*6)
<?php $dsn = new enru\DsnFromEnv(); $dsn_string = $dsn->parse();
A database connection can then easily be made:, (*7)
<?php
try {
$dsn = new enru\DsnFromEnv();
$dbh = new PDO($dsn->parse());
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}