我的 config.php Heroku 应用代码和 mysql 代码出错

Error in my config.php code for Heroku app and mysql

我正在为 PHP 使用 Heroku。我查看了我的 Heroku 日志并在我的 config.php 代码中发现了错误。我正在为我的编码组合制作一个 spotify 克隆,我用 xampp、apache、mysql 成功制作了它。但现在我需要展示它并且我必须使用 Heroku,因为它是免费且安全的。我在 Heroku 中添加了 cleardb mysql 的附加组件并连接了 MySQL workbench 并转储了 SQL。但是 运行 我在 heroku 上的应用程序出现此错误,“此页面无法正常工作 nowslotifyhokole.herokuapp.com 目前无法处理此请求。 HTTP 错误 500

我的配置php代码:

<?php
$url = parse_url(getenv("mysql://bde1900971353c:2b4ac360@us-cdbr-east-05.cleardb.net/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);
?>

我的 heroku 日志。

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 hokole551@gmail.com
2022-03-24T23:37:35.880770+00:00 app[api]: Deploy e5e872c1 by user hokole551@gmail.com
2022-03-24T23:37:35.880770+00:00 app[api]: Release v11 created by user hokole551@gmail.com
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 hokole551@gmail.com
2022-03-24T23:43:36.500807+00:00 app[api]: Release v12 created by user hokole551@gmail.com
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 hokole551@gmail.com
2022-03-24T23:44:02.953452+00:00 app[api]: Detach CLEARDB_WHITE (@ref:cleardb-defined-41840) by user hokole551@gmail.com
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

在你做任何其他事情之前,rotate those credentials.将它们从你的问题中编辑出来是不够。您已将它们发布到网上,它们将永远受到损害。


使用 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 的全部意义在于 包含 您的数据库 URL.

The ClearDB add-on sets such a variable for you: CLEARDB_DATABASE_URL.

<?php

$url = parse_url(getenv("CLEARDB_DATABASE_URL"));

不幸的是,MySQLi 看起来并不知道如何直接通过 URL 进行连接,因此您可以将 URL 分开并将各个值传递给构造函数。

但是您需要从刚刚从环境中加载的 URL 而不是 hard-coding 值来执行此操作。您已经在 URL 字符串上调用了 parse_url(),因此您应该已经拥有所需的内容。

像这样的东西应该可以工作:

$server = $url["host"];
$username = $url["user"];
$password = $url["pass"];
$db = substr($url["path"], 1);

$conn = new mysqli($server, $username, $password, $db);

抱歉,我刚刚弄明白为什么它不起作用。我没有部署到主分支。我必须先部署它。

证明有效:https://slotifyhokole.herokuapp.com/