简体   繁体   English

使用ClearDB在Heroku上部署PHP Web App

[英]Deploying PHP Web App on Heroku with ClearDB

So over the last few weeks I've been developing a PHP web app, and now that it's near completion I want to try to deploy it on Heroku. 因此,在过去的几周中,我一直在开发一个PHP Web应用程序,现在它已经接近完成,我想尝试将其部署在Heroku上。 I followed the tutorial on scotch.io , and it was fairly intuitive. 我遵循了scotch.io上的教程,它相当直观。 Until I got to the database connection. 直到我到达数据库连接。 I'll walk you through the steps I took, and maybe someone can point out where I've gone wrong. 我将引导您完成我所采取的步骤,也许有人可以指出我出了问题的地方。

Before I start I must mention I'm using PDO for the database connection and subsequent SQL queries. 在开始之前,我必须提到我正在使用PDO进行数据库连接和后续的SQL查询。 The scotch.io tutorial does not use PDO, so that's why I got derailed. scotch.io教程不使用PDO,所以这就是我出轨的原因。 Additionally, my app is live at the following URL: https://fathomless-bastion-62027.herokuapp.com/index.php 此外,我的应用程序位于以下URL上: https : //fathomless-bastion-62027.herokuapp.com/index.php

I got up to Step 11: Importing Local DB Tables into Heroku DB. 我已经完成了步骤11:将本地数据库表导入Heroku DB。 I ran the heroku config | grep CLEARDB_DATABASE_URL 我运行了heroku config | grep CLEARDB_DATABASE_URL heroku config | grep CLEARDB_DATABASE_URL command, obtained my cleardb URL, and as per this other scotch.io tutorial , I made my mysql command that was used to redirect output from my exported sql file from phpmyadmin. heroku config | grep CLEARDB_DATABASE_URL命令,获取了我的cleardb URL,并且根据其他scotch.io教程 ,我制作了mysql命令,该命令用于从phpmyadmin导出的sql文件中重定向输出。

I'm not sure if I did it right though, because if I go to ClearDB from the Heroku admin panel, and navigate to "Dev & Production Edition", nothing shows up. 我不确定我是否做对了,因为如果我从Heroku管理面板转到ClearDB,然后导航到“ Dev&Production Edition”,则不会显示任何内容。 But if I were to try rerunning the mysql command from Step 11, the cli gives me an error saying my tables already exist. 但是,如果我尝试从步骤11重新运行mysql命令,cli会给我一个错误,指出我的表已经存在。 I don't know, maybe I'm being paranoid, I've been trying to fix this for an hour+. 我不知道,也许我是偏执狂,我一直试图解决这个问题一个多小时。

Now we get to where the wheels started to fall off the wagon. 现在我们到达车轮开始从货车上掉落的位置。 The tutorial says we have to go update some database.php file, but I don't have one. 本教程说我们必须去更新一些database.php文件,但是我没有。 As I said above, I'm using PDO, and the way I learned PHP (took a web dev class last year college), all of our database connections were done with PHP snippets in our main files, so that's the way I did it for my app. 就像我上面说的,我正在使用PDO,以及我学习PHP的方式(去年是上大学的一个Web开发班),我们所有的数据库连接都是通过我们的主文件中的PHP代码段完成的,所以我就是这样做的为我的应用程序。 You can view my codebase HERE . 您可以在此处查看我的代码库。

So, I tried to establish the database connection in my index.php file based on Step 12 from the Scotch tutorial, and based my code on some other SO posts about ClearDB and PDO. 因此,我尝试根据Scotch教程中的步骤12在我的index.php文件中建立数据库连接,并将我的代码基于有关ClearDB和PDO的其他一些SO帖子中。 This is what I arrived at, and I don't really have any way of testing it. 这就是我得出的结论,但我实际上没有任何测试方法。 Also, this is located in my main index.php file, if that helps you pinpoint where I've gone wrong. 此外,这位于我的主要index.php文件中,如果可以帮助您查明我出了错的地方。

<?php 
//Get Heroku ClearDB connection information
$cleardb_url      = parse_url(getenv("CLEARDB_DATABASE_URL"));
$cleardb_server   = $cleardb_url["host"];
$cleardb_username = $cleardb_url["user"];
$cleardb_password = $cleardb_url["pass"];
$cleardb_db       = substr($cleardb_url["path"],1);

try {
    $pdo = new PDO("mysql:host=".$cleardb_server."; dbname=".$cleardb_db, $cleardb_username, $cleardb_password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo = null;
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}
?> 

This looks correct, though clearly it isn't because when you try to go to my web app, again accessible here: https://fathomless-bastion-62027.herokuapp.com/index.php , nothing shows up. 看起来是正确的,尽管显然不是,因为当您尝试转到我的Web应用程序时,可以再次在此处访问: https : //fathomless-bastion-62027.herokuapp.com/index.php ,没有任何显示。 And opening up Chrome Dev tools, I get a 500 ISE, Failed to Load Resources. 打开Chrome Dev工具,我得到500 ISE,无法加载资源。 I have a feeling my issue lies with my app's inability to connect to the database. 我感到我的问题在于我的应用无法连接数据库。 Apologies if what I'm asking seems trivial - this web app is more for my own learning purposes and if you more experienced PHP devs take a look at the git repo, you're probably going to find many glaringly obvious mistakes. 抱歉,如果我要问的是琐碎的-这个Web应用程序更多是出于我自己的学习目的,如果您有经验的PHP开发人员查看git repo,您​​可能会发现许多明显的错误。 They're bound to happen though, I'm fairly new to this. 尽管它们一定会发生,但我对此并不陌生。

If you've made it this far in my post and are able to help me out, I cannot thank you enough. 如果您在我的帖子中做到了这一点,并且能够为我提供帮助,那么我将感激不尽。 Any advice is appreciated. 任何建议表示赞赏。 Thank you! 谢谢!

-Dan -担

500 are PHP errors. 500是PHP错误。 You really need to check your error log file. 您确实需要检查错误日志文件。 If you don't know where the log file is located, use the phpinfo() command to dump your environment configuration and look for "error_log". 如果您不知道日志文件位于何处,请使用phpinfo()命令转储您的环境配置并查找“ error_log”。 My guess is you are not parsing CLEARDB_DATABASE_URL correctly, but, not knowing the value of it, I can't say what is wrong. 我的猜测是您没有正确解析CLEARDB_DATABASE_URL,但是,不知道它的值,我不能说出什么错了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM