I have used PHP to access MySQL databases numerous times for projects on our own server, and always use the same PHP script to do so (changing the login details obviously). However this is my first time using Heroku & ClearDB and I have run into problems.
The DB is on our Heroku PHP app using ClearDB, and I have accessed CLEARDB_DATABASE_URL to find out the username, password, hostname and database name. Just to be sure that I’ve done it correctly, my understanding is that this is the format:
mysql://USER_NAME:PASSWORD@HOST_NAME/DATABASE_NAME?reconnect=true
Using these details in MySQL Workbench I have managed to log into the DB and add a table, so I believe they are the correct details.
However when I try to access it using my PHP script, with the same variables taken from the URL, I get the following error:
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
At first I thought that maybe the Heroku app had gone done, but I can still access any of the functions in the PHP script that DO NOT attempt to access the database.
As I say, this script has always worked before so I am not sure where the problem lies.
My database connection is in a separate class with the following constructor:
function __construct()
{
$this->DB_HOST = 'us-cdbr-east-02.cleardb.com';
$this->DB_USERNAME = 'the_username_from_url';
$this->DB_PASSWORD = 'the_password_from_url';
$this->DB_DATABASE = 'the_database_name_from_url';
}
with a connect() function which is called as needed by other classes:
function connect()
{
self::$instance = new mysqli($this->DB_HOST, $this->DB_USERNAME, $this->DB_PASSWORD, $this->DB_DATABASE);
if (mysqli_connect_errno()) {
$this->raise_error(printf("Connect failed: %s\n", mysqli_connect_error()));
}
return self::$instance;
}
I am 99.9% certain that the problem is caused by attempting to connect to the database. Even though it does not return any PHP error messages when I test the script in a browser, it is only the DB based functions that cause the 500 error.
I’m open to any and all suggestions…
For those with the same issue, I have fixed this but am not 100% sure how, it was 1 of 2 possible things.
Firstly, I rebuilt my Heroku app using a custom buildpack. At first this didn’t work, so I presumed it was not the answer.
Then I left the Heroku app for a fortnight…and suddenly it was mySQLi compatible!
Either the custom buildpack DID work and just needed time to become active, or Heroku made changes at their end which fixed my problem.
Apologies that I cannot provide a better answer, but as I say the problem was miraculously fixed after leaving the Heroku app for about 2 weeks.