Wednesday, January 28, 2015

Why My PHP Scripts Did Not Work

It was late Wednesday afternoon. I was getting ready to go home after a long day at work when I decided to view the web based application I'm currently working on. I navigated to the location folder and opened my draft webpage. It was coded like this:

<!DOCTYPE html>
<html>
<head>
<style>
table,td, th {border: 3px inset red;}
</style>
</head>
<body>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "atdatabase";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);

echo "Connected successfully";

//retrieve data from database
$sql = "SELECT id, lastname, firstname FROM members ORDER By lastname";
$result = $conn->query($sql);

//if there are records, display them in an html table
if ($result->num_rows > 0) {
    // output data of each row
echo "<table>";
echo "<tr><th>" . "ID Number" . "</th>" . "<th>" . "Lastname" . "</th>" . "<th>" . "Firstname" . "</th></tr>"; 
    echo "<tr>";
while($row = $result->fetch_assoc()) {
 
        echo "<td>" . $row["id"]. "</td>" . "<td>" . $row["lastname"]. "</td>" . "<td>" . $row["firstname"]. "</td>";
echo "</tr>";
}
echo "</table>";
} else {
    echo "0 results";
}
//close the connection to mysql
$conn->close();
?>
</body>
</html>

According to my code, the page is supposed to retrieve data from a database and display it in a table on the webpage. But when I checked the output on my browser, I saw the html table, but the data I was expecting was nowhere to be found. I knew nothing was wrong with my code, because this was perfectly functional the day before. And no, I never changed anything on my original code. So that eliminates debugging errors as a potential solution to my dilemma.

The next suspect was PHP itself. I checked my WAMPP server and located the log for PHP errors. There I saw this line repeated multiple times: 

unable to load dynamic library 'c:/wamp/bin/php/php5.5.12/ext/php_ldap.dll'

Now how could this have happened. I never did anything new, except upgrade my Office version from 2007 to 2013. I thought maybe that's what got my PHP dll's messed up. So I tried searching for a possible fix on the net using the exact error message as my search keywords. I found several suggestions that according to their authors would help me solve the problem. Here are some of those suggested solutions:

  1. Check if the dll's mentioned in the error message are indeed in their correct folders. If they're not there, then download them and paste them on their correct locations.
  2. Add the php.exe path to the Path variable under Environment Variables.
  3. Reinstall the Visual C++ IE.
  4. Uninstall your WAMPP. Reinstall Visual C++ IE, and then reinstall your WAMPP.
Believe it or not, I tried all of them and yet none of them worked. I checked my PHP log and saw that the error message was no longer there. Therefore, it was no longer PHP's fault. But upon opening my test webpage on a browser, I still didn't see the data which was supposedly to be retrieved from the database and displayed in a table. I was so frustrated. I decided to let this go for the meantime. I shut my PC down and went home like a wounded warrior from a loosing battle.

Then came the following morning. I still couldn't get my mind off the issue like it was some thorn poking my throat. I had to get rid of it. I had to solve it. I became desperate. It was time to use "System Restore". If I couldn't solve this, then I might as well travel back in time when everything was running perfectly. That was exactly 2 days ago. I opened the System Restore app, and was ready to gain back my sanity when lo and behold, I had zero restore points. Blank. Null. Nothing. I was already desperate, and by now I was also feeling hopeless. I thought, there's no shame in giving up. I could just succumb to the wrath of whatever digital devil has latched onto me. I should just lay down the sword, and surrender. And then occurred to me. Like a blinding light, it flashed through my brain. I saw it out of thin air. Holy crap how could I have been so stupid. That was it. That was the answer. How in the world could I have forgotten, to type "localhost" on the address bar before the actual name of the php file. It wasn't missing dll's, nor an incorrect path variable, nor a corrupted Visual C++ ie that messed up my webpage. It was my own brain that did it. Yes, I am blaming my brain. It played a trick on me, and it got me good.

The moral lesson of the story: Not all problems that look complicated, require a complicated solution. Sometimes, you'll realize that you knew the answer all along.