Talk about a task at hand when working through your first course on PHP code… This stuff is a huge pain in the arse when trying to follow along in a book that isn’t always compatible with what server you are currently working with while implementing MySQL. However, I am a little fortunate in the fact that my browser did throw a bunch of helpful exceptions at me when trying to work through the kinks of getting clean code to the browser for accurate interpretation.
First off, the syntax of PHP is relatively easy to spot when you have a minor error such as a missing semicolon, closing parenthesis or missing bracket. The trickier part is when intertwining MySQL into the mix, you can get errors on lines that don’t always match up to where the browser may hint to you. The key to deciphering these (I’ve found out) is to check your query first off. A small typo can throw the whole thing off and throw an exception at you that you don’t have a valid query. Another key thing to check for, is that you don’t have typos when trying to link to your database. Be sure to check that you have the proper database name, table name, and password lined up correctly. It’s always a good thing to start off with a very basic SELECT * FROM XxdatabasexX and be sure that your connection is set up properly. If your database connection is set up properly then you can proceed to throw more advanced queries that way and see what happens. Another nice thing when using phpMyAdmin is the fact that if you are having trouble using your PHP code to interact with MySQL, then you can bypass that directly with your query and just manually type your query in to the phpMyAdmin interface to see that you are coming up with a valid query anyway!
And don’t be afraid to insert a bunch of echo statements in the middle of loops, etc. to see what loops your code is executing and get the value of different variables only to confirm that they actually are holding the values that you expect them to, in order to accurately execute the code you expect them to!
There are a bunch of troubleshooting techniques with all sorts of code developing, and these are just a few of the techniques that I found helpful in my struggles with PHP and MySQL!