CodeIgniter Blank Page Fix: Blank webpage when setting up CodeIgniter, PHP, MySQL, and Apache

Today, I created my first website using the Code Igniter PHP framework and, oh boy, do I like it a lot more than Ruby on Rails. But I’ll dig into that further in a future post.

The purpose of this post is to shed some light on a problem that I wracked my brain over for a few hours last night.

The problem

After installing Apache, PHP, and MySQL and configuring the Code Igniter framework, you get a blank web page when testing your first controller, model, and view.

Solution

What I discovered were two issues that, when combined, create the perfect storm of an installation problem:

  1. First, MySQL is no longer enabled by default with PHP 5. Says the PHP website of MySQL in PHP 5:
    MySQL is no longer enabled by default, so the php_mysql.dll DLL must be enabled inside of php.ini. Also, PHP needs access to the MySQL client library. A file named libmysql.dll is included in the Windows PHP distribution and in order for PHP to talk to MySQL this file needs to be available to the Windows systems PATH.It’s been a LONG time since I’ve programmed in PHP let alone installed PHP, so this was news to me. Anyway, if check out the MySQL installation notes on the PHP website if you need help enabling MySQL.  
  2. Unfortunately, that was the easy part. Once I had an error message to work with, Google lead me to the PHP website, and I was in the clear. But, for the longest time, I wasn’t even getting an error page. Remember, all I was getting was a blank HTML page. So what was the cause? Code Igniter…
  3. Using some creatively placed die() calls, I found out that the application was dying when trying to connect to the database. (Previous to this, I had already double- and triple-checked my MySQL username and password in the Code Igniter configuration file, and had even tried out alternate accounts, like my MySQL root account.)What I found out was that the MySQL drivers in Code Igniter were suppressing errors when attempting database connections.

This is what the code looks like in the Code Igniter mysql_driver.php file:

function db_connect()
{
   if ($this->port != '')
   {
      $this->hostname .= ':'.$this->port;
   }
   return @mysql_connect($this->hostname, $this->username, $this->password, TRUE);
}


The “@” symbol before the mysql_connect() function call supresses any errors that may be returned. It is that symbol that wasted a good 2 hours of my life.Anyway, after I removed the “@” symbol from the code and re-tested my web application, PHP spit out the error message that I should have been presented with hours ago, and I was well on my way to fixing the problem.
 If you’re experiencing the same problem that I was, I hope this helps you correct it faster than I did! Best of luck.

Oh, and did I mention how much I prefer Code Igniter over Ruby on Rails? It’s messier, but with my C and Java background, it makes more sense than Ruby and Ruby on Rails does!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *