Heroku应用程序和mysql的config.php代码中出现错误



我使用的是Heroku for PHP。我查看了我的Heroku日志,在config.php代码中发现了错误。我正在为我的编码组合制作一个spotify克隆,它是我用xampp、apache、mysql成功制作的。但现在我需要展示它,我必须使用Heroku,因为它是免费和安全的。我在Heroku中添加了cleardb-mysql的附加组件,连接了mysql工作台并转储了SQL。然而,在heroku上运行我的应用程序时,我收到了这个错误,";此页面目前不工作slotifyhokole.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:xampphtdocsSlotify>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

在执行其他操作之前,请先轮换这些凭据在没有问题的情况下编辑它们是不够的。你已经在网上发布了它们,它们永远都会被破坏。


使用getenv()作为凭据的全部意义在于,您不必将它们包含在源代码中。getenv()的参数不应该是您的数据库URL;它应该是包含数据库URL的环境变量的名称。

ClearDB加载项为您设置了这样一个变量:CLEARDB_DATABASE_URL

<?php
$url = parse_url(getenv("CLEARDB_DATABASE_URL"));

不幸的是,MySQLi似乎不知道如何直接通过URL进行连接,所以将URL拆开并将单个值传递给构造函数是正确的。

但是,您需要从刚刚从环境中加载的URL中执行此操作,而不是对值进行硬编码。您已经在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/

最新更新