|
Introduction to Debugging and Error Checking |
|
|
|
Page 1 of 3 Introduction to Debugging and Error Checking Debugging essentially means to track down an error in your code. Found a "bug" in your code? Then you need to "de-bug" it .
What is debugging? Debugging essentially means to track down an error in your code. Found a "bug" in your code? Then you need to "de-bug" it  This article will introduce you to some basic concepts such as error checking and built-in functions like var_dump() that will aid you in tracking down errors in your PHP applications. There are also many fully-fledged debugging tools for PHP that lets you set breakpoints, watch variables, and such like, but we won't be using them in this article Number One Rule: Always Check This is my number one rule when writing applications. Whenever your code does something that can potentially fail, you should always add a check to your code to see if it did fail. For example, in the following code, we take the URL parameter 'name' and use it to display a personal greeting to our visitor: PHP Code: <?php $name = $_GET['name']; echo 'Hello ' . $name . ', welcome to our website!'; Here we can see that if we ran the script by using: Code: http://www.example.com/myScript.php?name=Alan It would print: Code: Hello Alan, welcome to our website! in the browser. However, what happens if we don't specify a name? We would then end up with something like: Code: Hello , welcome to our website! which I'm sure you'll agree doesn't look very professional at all! To solve this problem, we need to perform a check to see if the name has been supplied. Lets edit our script so that it looks like the following: PHP Code: <?php if ($_GET['name']) { $name = $_GET['name']; } else { $name = 'Visitor'; } echo 'Hello ' . $name . ', welcome to our website!'; In this code, we can now see that our script will check to see if $_GET['name'] has a value. If it has, it puts that value into our $name variable. If it doesn't have a value, it sets our $name variable to a default value, in this case "Visitor". Now if we run our script without supplying a name, the following would appear: Code: Hello Visitor, welcome to our website! Whilst the script above isn't likely to produce any bugs in our code, we can see the importance of checking for problems. As a second example, we will use MySQL to fetch the top 5 articles from a table then display their titles. Take a look at the following code: PHP Code: <?php // Connect to our database and select the 'myDatabase' database $db = mysql_connect('localhost', 'username', 'password'); mysql_query('USE myDatabase'); // Fetch the top 5 article ids and titles from our rating table $top5_q = "SELECT id, title, MAX(rating) FROM rating ORDER BY articles DESC LIMIT 5"; $top5_r = mysql_query($top5_q); // Loop through our results... while ($top5_a = mysql_fetch_array($top5_r)) { // ...and echo each articles title echo $top5_a['title'] . '<br />'; } Fairly straight-forward bit of code here, we select the articles then echo the titles. But hold on, something's wrong - our script isn't echoing the article titles like it should! This is why checking that a function was successful is so important, in our code above we have a bug but due to the lack of checks, we have no idea where it could be. Now, lets add in some checks so we can find out where the problem is occurring. First off all, we need to check that we even connected to the database server. If that didn't happen, the rest of the code will fail. PHP Code: <?php // Connect to our database and select he 'myDatabase' database $db = mysql_connect('localhost', 'username', 'password'); // Check that we connected ok... if (!db) { // ...Nope! Display the MySQL error die('Could not connect to the database: ' . mysql_error()); } As you can see, we now have that all important check in the code. If $db is false (ie, the connect failed), we display an error message. In our case we use the die() function which immediately ends the script and tell it to display some text then call the mysql_error() function.
|