enru / dsnfromenv
Installs: 81
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/enru/dsnfromenv
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2026-01-25 09:05:13 UTC
README
Parses a database URL from the environment into a DSN connection string for use in PHP's PDO.
Useful for parsing heroku config DATABASE_URL variable.
e.g.
A database URL set in an environment variable:
export DATABASE_URL=postgres://USER:PASS@HOST:PORT/DBNAME
...can be parsed into this string:
'pgsql:host=HOST;port=PORT;user=USER;dbname=DBNAME;password=PASSWORD'
...using this code:
<?php
$dsn = new enru\DsnFromEnv();
$dsn_string = $dsn->parse();
A database connection can then easily be made:
<?php
try {
$dsn = new enru\DsnFromEnv();
$dbh = new PDO($dsn->parse());
}
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}