'Error in my config.php code for Heroku app and mysql
I am using Heroku for PHP. I looked at my Heroku logs and found errors in my config.php code. I am making a spotify clone for my coding portfolio which I made successfully with xampp, apache, mysql. But now I need to showcase it and I have to use Heroku since it is free and secure. I added the add on for cleardb mysql in Heroku and connected the MySQL workbench and dumped the SQL. However running my app on heroku I get this error, "This page isn’t working right nowslotifyhokole.herokuapp.com can't currently handle this request. HTTP ERROR 500"
My config php code:
<?php
$url = parse_url(getenv("mysql://bde1900971353c:[email protected]/heroku_d347ebb2241490f?reconnect=true"));
$server = "us-cdbr-east-05.cleardb.net";
$username = "bde1900971353c";
$password = "2b4ac360";
$db = substr("heroku_d347ebb2241490f", 1);
$conn = new mysqli($server, $username, $password, $db);
?>
My heroku logs.
C:\xampp\htdocs\Slotify>heroku logs
» Warning: heroku update available from 7.53.0 to 7.59.4.
2022-03-24T23:37:22.295617+00:00 app[web.1]: #2 /app/includes/includedFiles.php(20): include('...')
2022-03-24T23:37:22.295696+00:00 app[web.1]: #3 /app/index.php(2): include('...')
2022-03-24T23:37:22.295722+00:00 app[web.1]: #4 {main}
2022-03-24T23:37:22.295840+00:00 app[web.1]: thrown in /app/includes/config.php on line 7
2022-03-24T23:37:22.296307+00:00 app[web.1]: 10.1.24.9 - - [24/Mar/2022:23:37:22 +0000] "GET / HTTP/1.1" 500 - "https://dashboard.heroku.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.46
2022-03-24T23:37:23.000000+00:00 app[api]: Build started by user [email protected]
2022-03-24T23:37:35.880770+00:00 app[api]: Deploy e5e872c1 by user [email protected]
2022-03-24T23:37:35.880770+00:00 app[api]: Release v11 created by user [email protected]
2022-03-24T23:37:36.000000+00:00 app[api]: Build succeeded
2022-03-24T23:37:37.192793+00:00 heroku[web.1]: Restarting
2022-03-24T23:37:37.210181+00:00 heroku[web.1]: State changed from up to starting
2022-03-24T23:37:38.063702+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2022-03-24T23:37:38.127025+00:00 app[web.1]: SIGTERM received, attempting graceful shutdown...
2022-03-24T23:37:38.127379+00:00 app[web.1]: Stopping httpd gracefully...
2022-03-24T23:37:39.141718+00:00 app[web.1]: Stopping php-fpm gracefully...
2022-03-24T23:37:39.161423+00:00 app[web.1]: Shutdown complete.
2022-03-24T23:37:39.298754+00:00 heroku[web.1]: Process exited with status 143
2022-03-24T23:37:40.385404+00:00 heroku[web.1]: Starting process with command `heroku-php-apache2`
2022-03-24T23:37:42.372921+00:00 app[web.1]: Detected 536870912 Bytes of RAM
2022-03-24T23:37:42.401935+00:00 app[web.1]: PHP memory_limit is 128M Bytes
2022-03-24T23:37:42.409524+00:00 app[web.1]: Starting php-fpm with 4 workers...
2022-03-24T23:37:42.583116+00:00 app[web.1]: Starting httpd...
2022-03-24T23:37:42.915564+00:00 heroku[web.1]: State changed from starting to up
2022-03-24T23:37:43.583802+00:00 app[web.1]: [24-Mar-2022 23:37:43 UTC] PHP Warning: Undefined array key "us-cdbr-east-05.cleardb.net" in /app/includes/config.php on line 4
2022-03-24T23:37:43.583985+00:00 app[web.1]: [24-Mar-2022 23:37:43 UTC] PHP Warning: Undefined array key "bde1900971353c" in /app/includes/config.php on line 5
2022-03-24T23:37:43.584173+00:00 app[web.1]: [24-Mar-2022 23:37:43 UTC] PHP Warning: Undefined array key "2b4ac360" in /app/includes/config.php on line 6
2022-03-24T23:37:43.584273+00:00 heroku[router]: at=info method=GET path="/" host=slotifyhokole.herokuapp.com request_id=e2683045-460e-4e5d-86b2-aee6b3825147 fwd="104.54.13.160" dyno=web.1 connect=0ms service=2ms status=500 bytes=169 protocol=https
2022-03-24T23:37:43.584383+00:00 app[web.1]: [24-Mar-2022 23:37:43 UTC] PHP Warning: Undefined array key "heroku_d347ebb2241490f" in /app/includes/config.php on line 7
2022-03-24T23:37:43.584658+00:00 app[web.1]: [24-Mar-2022 23:37:43 UTC] PHP Deprecated: substr(): Passing null to parameter #1 ($string) of type string is deprecated in /app/includes/config.php on line 7
2022-03-24T23:37:43.584880+00:00 app[web.1]: [24-Mar-2022 23:37:43 UTC] PHP Fatal error: Uncaught mysqli_sql_exception: No such file or directory in /app/includes/config.php:9
2022-03-24T23:37:43.584907+00:00 app[web.1]: Stack trace:
2022-03-24T23:37:43.585002+00:00 app[web.1]: #0 /app/includes/config.php(9): mysqli->__construct()
2022-03-24T23:37:43.585083+00:00 app[web.1]: #1 /app/includes/header.php(2): include('...')
2022-03-24T23:37:43.585178+00:00 app[web.1]: #2 /app/includes/includedFiles.php(20): include('...')
2022-03-24T23:37:43.585246+00:00 app[web.1]: #3 /app/index.php(2): include('...')
2022-03-24T23:37:43.585275+00:00 app[web.1]: #4 {main}
2022-03-24T23:37:43.585357+00:00 app[web.1]: thrown in /app/includes/config.php on line 9
2022-03-24T23:37:43.585772+00:00 app[web.1]: 10.1.91.104 - - [24/Mar/2022:23:37:43 +0000] "GET / HTTP/1.1" 500 - "https://dashboard.heroku.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.46
2022-03-24T23:43:36.500807+00:00 app[api]: Detach CLEARDB_SILVER (@ref:cleardb-triangular-75679) by user [email protected]
2022-03-24T23:43:36.500807+00:00 app[api]: Release v12 created by user [email protected]
2022-03-24T23:43:36.754002+00:00 heroku[web.1]: Restarting
2022-03-24T23:43:36.756756+00:00 heroku[web.1]: State changed from up to starting
2022-03-24T23:43:37.837151+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2022-03-24T23:43:37.927033+00:00 app[web.1]: SIGTERM received, attempting graceful shutdown...
2022-03-24T23:43:37.927390+00:00 app[web.1]: Stopping httpd gracefully...
2022-03-24T23:43:38.957430+00:00 app[web.1]: Stopping php-fpm gracefully...
2022-03-24T23:43:39.014316+00:00 app[web.1]: Shutdown complete.
2022-03-24T23:43:39.190036+00:00 heroku[web.1]: Process exited with status 143
2022-03-24T23:43:41.179084+00:00 heroku[web.1]: Starting process with command `heroku-php-apache2`
2022-03-24T23:43:42.867065+00:00 app[web.1]: Detected 536870912 Bytes of RAM
2022-03-24T23:43:42.897211+00:00 app[web.1]: PHP memory_limit is 128M Bytes
2022-03-24T23:43:42.903059+00:00 app[web.1]: Starting php-fpm with 4 workers...
2022-03-24T23:43:42.976329+00:00 app[web.1]: Starting httpd...
2022-03-24T23:43:43.414865+00:00 heroku[web.1]: State changed from starting to up
2022-03-24T23:44:02.953452+00:00 app[api]: Release v13 created by user [email protected]
2022-03-24T23:44:02.953452+00:00 app[api]: Detach CLEARDB_WHITE (@ref:cleardb-defined-41840) by user [email protected]
2022-03-24T23:44:03.242155+00:00 heroku[web.1]: Restarting
2022-03-24T23:44:03.257003+00:00 heroku[web.1]: State changed from up to starting
2022-03-24T23:44:04.079660+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2022-03-24T23:44:04.152495+00:00 app[web.1]: SIGTERM received, attempting graceful shutdown...
2022-03-24T23:44:04.152819+00:00 app[web.1]: Stopping httpd gracefully...
2022-03-24T23:44:05.167068+00:00 app[web.1]: Stopping php-fpm gracefully...
2022-03-24T23:44:05.188562+00:00 app[web.1]: Shutdown complete.
2022-03-24T23:44:05.341007+00:00 heroku[web.1]: Process exited with status 143
2022-03-24T23:44:06.375641+00:00 heroku[web.1]: Starting process with command `heroku-php-apache2`
2022-03-24T23:44:08.058223+00:00 app[web.1]: Detected 536870912 Bytes of RAM
2022-03-24T23:44:08.091303+00:00 app[web.1]: PHP memory_limit is 128M Bytes
2022-03-24T23:44:08.098347+00:00 app[web.1]: Starting php-fpm with 4 workers...
2022-03-24T23:44:08.177733+00:00 app[web.1]: Starting httpd...
2022-03-24T23:44:08.608485+00:00 heroku[web.1]: State changed from starting to up
2022-03-24T23:44:09.037497+00:00 app[web.1]: [24-Mar-2022 23:44:09 UTC] PHP Warning: Undefined array key "us-cdbr-east-05.cleardb.net" in /app/includes/config.php on line 4
2022-03-24T23:44:09.037797+00:00 app[web.1]: [24-Mar-2022 23:44:09 UTC] PHP Warning: Undefined array key "bde1900971353c" in /app/includes/config.php on line 5
2022-03-24T23:44:09.037985+00:00 app[web.1]: [24-Mar-2022 23:44:09 UTC] PHP Warning: Undefined array key "2b4ac360" in /app/includes/config.php on line 6
2022-03-24T23:44:09.038204+00:00 app[web.1]: [24-Mar-2022 23:44:09 UTC] PHP Warning: Undefined array key "heroku_d347ebb2241490f" in /app/includes/config.php on line 7
2022-03-24T23:44:09.038446+00:00 heroku[router]: at=info method=GET path="/" host=slotifyhokole.herokuapp.com request_id=790f63f6-d998-4e0d-8f30-93c186bc506f fwd="104.54.13.160" dyno=web.1 connect=0ms service=2ms status=500 bytes=169 protocol=https
2022-03-24T23:44:09.038486+00:00 app[web.1]: [24-Mar-2022 23:44:09 UTC] PHP Deprecated: substr(): Passing null to parameter #1 ($string) of type string is deprecated in /app/includes/config.php on line 7
2022-03-24T23:44:09.038736+00:00 app[web.1]: [24-Mar-2022 23:44:09 UTC] PHP Fatal error: Uncaught mysqli_sql_exception: No such file or directory in /app/includes/config.php:9
2022-03-24T23:44:09.038753+00:00 app[web.1]: Stack trace:
2022-03-24T23:44:09.038853+00:00 app[web.1]: #0 /app/includes/config.php(9): mysqli->__construct()
2022-03-24T23:44:09.038940+00:00 app[web.1]: #1 /app/includes/header.php(2): include('...')
2022-03-24T23:44:09.039046+00:00 app[web.1]: #2 /app/includes/includedFiles.php(20): include('...')
2022-03-24T23:44:09.039118+00:00 app[web.1]: #3 /app/index.php(2): include('...')
2022-03-24T23:44:09.039141+00:00 app[web.1]: #4 {main}
2022-03-24T23:44:09.039228+00:00 app[web.1]: thrown in /app/includes/config.php on line 9
2022-03-24T23:44:09.039679+00:00 app[web.1]: 10.1.57.226 - - [24/Mar/2022:23:44:09 +0000] "GET / HTTP/1.1" 500 - "https://dashboard.heroku.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.46
2022-03-25T00:01:48.843149+00:00 app[web.1]: [25-Mar-2022 00:01:48 UTC] PHP Warning: Undefined array key "us-cdbr-east-05.cleardb.net" in /app/includes/config.php on line 4
2022-03-25T00:01:48.843349+00:00 app[web.1]: [25-Mar-2022 00:01:48 UTC] PHP Warning: Undefined array key "bde1900971353c" in /app/includes/config.php on line 5
2022-03-25T00:01:48.843543+00:00 app[web.1]: [25-Mar-2022 00:01:48 UTC] PHP Warning: Undefined array key "2b4ac360" in /app/includes/config.php on line 6
2022-03-25T00:01:48.843747+00:00 app[web.1]: [25-Mar-2022 00:01:48 UTC] PHP Warning: Undefined array key "heroku_d347ebb2241490f" in /app/includes/config.php on line 7
2022-03-25T00:01:48.843795+00:00 heroku[router]: at=info method=GET path="/" host=slotifyhokole.herokuapp.com request_id=bc2431a6-211f-4ed5-8712-23e77ebb128e fwd="104.54.13.160" dyno=web.1 connect=0ms service=1ms status=500 bytes=169 protocol=https
2022-03-25T00:01:48.844048+00:00 app[web.1]: [25-Mar-2022 00:01:48 UTC] PHP Deprecated: substr(): Passing null to parameter #1 ($string) of type string is deprecated in /app/includes/config.php on line 7
2022-03-25T00:01:48.844309+00:00 app[web.1]: [25-Mar-2022 00:01:48 UTC] PHP Fatal error: Uncaught mysqli_sql_exception: No such file or directory in /app/includes/config.php:9
2022-03-25T00:01:48.844341+00:00 app[web.1]: Stack trace:
2022-03-25T00:01:48.844450+00:00 app[web.1]: #0 /app/includes/config.php(9): mysqli->__construct()
2022-03-25T00:01:48.844554+00:00 app[web.1]: #1 /app/includes/header.php(2): include('...')
2022-03-25T00:01:48.844662+00:00 app[web.1]: #2 /app/includes/includedFiles.php(20): include('...')
2022-03-25T00:01:48.844739+00:00 app[web.1]: #3 /app/index.php(2): include('...')
2022-03-25T00:01:48.844764+00:00 app[web.1]: #4 {main}
2022-03-25T00:01:48.844861+00:00 app[web.1]: thrown in /app/includes/config.php on line 9
2022-03-25T00:01:48.845404+00:00 app[web.1]: 10.1.57.71 - - [25/Mar/2022:00:01:48 +0000] "GET / HTTP/1.1" 500 - "https://dashboard.heroku.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36 Edg/99.0.1150.46
Solution 1:[1]
Before you do anything else, rotate those credentials. Editing them out of your question is not enough. You have published them online and they are forever compromised.
The whole point of using getenv() for credentials is so you don't have to include them in your source code. The argument to getenv() shouldn't be your database URL; it should be the name of an environment variable that contains your database URL.
The ClearDB add-on sets such a variable for you: CLEARDB_DATABASE_URL.
<?php
$url = parse_url(getenv("CLEARDB_DATABASE_URL"));
Unfortunately, it doesn't look like MySQLi knows how to connect via a URL directly, so you're right to pull the URL apart and pass individual values into the constructor.
But you need to do it from the URL you have just loaded from the environment instead of hard-coding values. You've already called parse_url() on the URL string, so you should already have what you need.
Something like this should work:
$server = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$db = substr($url["path"], 1);
$conn = new mysqli($server, $username, $password, $db);
Solution 2:[2]
Sorry, but I just figured out why it wouldn't work. I didn't deploy to the main branch. I had to deploy it first.
Proof it works: https://slotifyhokole.herokuapp.com/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Chris |
| Solution 2 | Hoko A |
