Dailytip.net

Articles on pretty much anything.

About the author

Author Name is someone.
E-mail me Send mail

Recent posts

Recent comments

Tags

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2009


Debugging in PHP

Many a time, whilst scripting in PHP, I've hit a problem with data. Usually it's some silly fault I made in the sending or processing and other times I've forgotten to take out the remnants of an older piece of code which causes the data to act in an unwanted manner. When I hit these problems, the first thing I do is try and debug it by looking at the data before and after it has been passed. In the article I'd like to cover some of the techniques I find most useful.

I'm going to assume you have sufficient knowledge of PHP to understand what I'm doing in these examples. First of all ,let's look at a simple example such as a login. Here's the scenario:

A user (you) has input the username and password, which you know are correct, but the script isn't logging you in.

It's at this point I would take a look at the data being passed to make sure there are no changes. To do this I would,first, comment out any SQL queries being done. For example:

 


0:  $q = mysql_query("SELECT * FROM `user_table` WHERE `Username` = '$user' AND `Password` = '$password'");
1:  $query = mysql_fetch_assoc($q);

2:  /* Becomes */

3:  //$q = mysql_query("SELECT * FROM `user_table` WHERE `Username` = '$user' AND `Password` = '$password'");
4:  //$query = mysql_fetch_assoc($q);

 

I would also do the same thing to any hashing processes on passwords and such.

Once I've eliminated the queries temporarily, I can start accessing the data. In this scenario I'm going to assume I've sent the data via post. It can therefore be accessed via $_POST. To access it I would use this code on the page which the login goes to:


0:   print_r($_POST);
1:   //I could also use this to make the formatting easier for me to read
2:   foreach($_POST as $post => $val)
3:   {
4:     echo($post."=>".$val);
5:     echo("<br />");
6:   }

I can compare this list of data to the data I put it and see if there are any changes. If there I can track down the code that causes them in the source by commenting, and un-commenting, lines in the source.
 
If there aren't any changes in the data passed it will most likely be, in this scenario, the SQL query that is causing the error. There's a very quick and simple way of testing whether or not an SQL query was the problem.




1:  if(!$q) //This ($q) is the query we used earlier
2:  {
3:   /** error handler */
4:  }

5:  if(!mysql_num_rows($q))
6:  {
7:   /** No results returned */
8:  }
9:  else
10: {
11:   while($row = mysql_fetch_object($q))
12:   {
13:   /** $row->${...} */
14:   }
15:  mysql_free_result($q);
16: }
 

If the query has failed and I'm presented with the error message I specified in the script, then I know I need to fix up my query; but that's not for this article.

I hope this article has helped you at least a little. I apologise for the lack of syntax highlighting. We will be getting this issue sorted in V2 of DailyTip.

Note: All SQL queries here are for demonstration purposes.

  

Currently rated 4.3 by 3 people

  • Currently 4.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: How-tos
Posted by Andy on Sunday, March 23, 2008 12:58 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Monday, January 05, 2009 5:27 PM