How to make connection to heroku mysql database

https://stackoverflow.com/questions/32109099/how-to-make-connection-to-heroku-mysql-database

Ask QuestionAsked 4 years, 5 months agoActive 4 years, 5 months agoViewed 747 times1

I created a web app that I was able to successfully deploy to heroku. The site uses a mysql database that I dumped into the project directory for the app. I followed the instructions from this SO post as well as other online sources. I entered the following command into the terminal:

 heroku config | grep CLEARDB_DATABASE_URL

and got the CLEARDB_DATABASE_URL: mysql://be4beb15e4506b:f601032d@us-cdbr-iron-east-02.cleardb.net/heroku_e665c17e181f638?reconnect=true

Before deploying the app I then entered:

 mysql -u be4beb15e4506b  -h us-cdbr-iron-east-02.cleardb.net -p heroku_e665c17e181f638 < /var/www/html/Clubhub.sql

Inside of the actual files, I added the following code for the database connection:

url = parse_url(getenv(" mysql://be4beb15e4506b:f601032d@us-cdbr-iron-east-02.cleardb.net/heroku_e665c17e181f638?reconnect=true"));
$server = $url["us-cdbr-iron-east-02.cleardb.net"];
$username = $url["be4beb15e4506b"];
$password = $url["heroku_e665c17e181f638"];
$db = substr($url["/var/www/html/Clubhub.sql"], 1);
$connection = new mysqli($server, $username, $password, $db);

For some reason whenever I navigate to a page where a query is done, I'm getting a connection error. Based on the url provided, have the correct arguments been passed to the connection?php mysql database heroku deploymentshareimprove this questionedited May 23 '17 at 12:22Community♦111 silver badgeasked Aug 20 '15 at 3:29loremIpsum17712,11911 gold badge2121 silver badges5454 bronze badges

add a comment

1 Answer

activeoldestvotes1

I found the answer on server fault here

The correct format for the database connection is:

$url = parse_url(getenv("CLEARDB_DATABASE_URL"));
$server = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$db = substr($url["path"], 1);
$connection = new mysqli($server, $username, $password, $db);

Last updated

Was this helpful?