PHP is a strange animal, as I’ve noted before. And, since most of my webdesign and development experience is self-taught and I’ve mostly worked with front-end design in HTML and CSS, PHP has been more of a frustrating puzzle than a new horizon in my coding skills.
Because of this, I’ve run into a couple of PHP fails in my attempts to teach myself this new language. That story follows!
Problem: Can’t Search My Own Database
I couldn’t understand why my variable-laden code for a simple database search wasn’t working, since I had gotten the majority of the code off a fairly reputable PHP code website, and I thought I’d input all the variables correctly. But the code continued to return an error, saying that the database was not “a valid result resource.”
Debugging with a Good Friend
One of my good friends is a computer programmer by nature, and though he knew little of PHP at the time, he was able to express one of the fundamental truths of PHP in a way I could understand it. “Basically, PHP sounds like a function-based language,” he said. “You tell it things to do–functions–based on the variables and values you give it.”
What this meant to me: if the variable isn’t right, or you haven’t got a way to give the variable any value, you’re in trouble! Certainly I had already run into that problem when I was trying to make the PHP code search the MySQL database; the darn thing just wouldn’t budge, and now I knew at least one reason why.
Solution: You MUST Be VERY Specific When You Work with PHP
Once I finally understood that I had to give PHP a variable’s value before I could ask it to make that variable jump through flaming hoops, one of the main problems in my searchable database became clearer: somehow, one of the variables that related to the database was not being given a correct value. Otherwise, what else could be making the database an “invalid result resource?” (We eventually discovered that the database connection itself was to blame–I had mistyped ONE comma as a period, and the whole code had gone bonkers as a result.)
It may seem like common sense to people who have already mastered PHP and MySQL, but for a non-mathematical person who would have preferred to leave variables back in algebra where they belong, it was a very tough hurdle to jump. Even realizing this small piece of information was a victory.
Whenever you work with a highly technical language like PHP, remember that it is unforgiving of most errors. Double- and triple-check your code, testing it often, to make sure your changes actually work. And please, for the sake of your eyeballs and blood pressure, make sure your database connection works so that your database variable has a proper value!
Next Up: The Triumphant Fixed Database
Thankfully, this wasn’t the end of the story! Head on over to Part 2 of this article to see how we transformed this broken database script into a functioning one! (Samples of PHP code, oh my!)
For More Info: