PhpRiot
Download This Article
Download this article in PDF format with all listings and files.

Price: $5.00 AUD
(Approx. $4.45 USD)

More information
Related Books
Ajax for Web Application Developers (Developer's Library)

Ajax for Web Application Developers (Developer's Library)

Reusable components and patterns for Ajax-driven applications Ajax is one of the latest...

Zend Studio for Eclipse Developer's Guide (Developer's Library)

Zend Studio for Eclipse Developer's Guide (Developer's Library)

The definitive, comprehensive guide to Zend Studio for Eclipse Zend Studio for Eclipse...
Browse Articles
Ajax (4), APC (1), CAPTCHA (1), CSS (3), Debugging (1), File Upload (1), Google (3), Google Maps (2), JavaScript (11), JSON (2), MVC (1), MySQL (6), onbeforeunload (1), OOP (1), PHP (27), PhpDoc (1), PostgreSQL (6), Prototype (10), Reflection (1), RFC 1867 (1), Robots (1), Scriptaculous (1), SEO (1), Sessions (1), SimpleXML (1), Smarty (5), SOAP (1), SPL (1), Templates (2), W3C (1), XHTML (1), Zend Framework (1), Zend_Search_Lucene (1)

PhpRiot Newsletter
Your Email Address:

Debugging Your Web Application

Recording Debug Information

The final thing you must do now is record the actual debugging information.

You must determine at which points in your code you want to write your debugging information.

As an example, I’ll show you how to use it for the SQL debug switches.

Debugging SQL

Let’s say you have an SQL abstraction class like this:

Listing 7 listing-7.php
<?php
    class DB
    {
        var $conn;
 
        function query($query)
        {
            $result = pg_query($this->conn, $query);
 
            return $result;
        }
    }
?>

Obviously this is oversimplified, but hopefully you get the idea.

To add debugging to this, you would modify the query() method something like this:

Listing 8 listing-8.php
<?php
    class DB
    {
        var $conn;
 
        function query($query)
        {
            $query = trim($query);
            if (preg_match("/^select/i", $this->query)) {
                debug(DEBUG_SQL_SELECT, 'SQL SELECT: ' . $query);
            }
            $result = pg_query($this->conn, $query);
            if (!$result) {
                $errmsg = pg_last_error($this->conn);
                debug(DEBUG_SQL_ERROR, 'SQL Error: ' . $errmsg . ' -- query: ' . $query);
            }
 
            return $result;
        }
    }
?>

This adds debugging for errors and for select statements. It is easy to add debugging for inserts, updates and deletes based on the select code. The preg_match() calls determines whether or not the query is a select query.

So if an error occurred, and the DEBUG_LEVEL included DEBUG_SQL_ERROR, then the error message would appear in the log file specified is DEBUG_LOG_FILE.

Based on this code it is not too difficult to implement debugging to the other portions of your code. Remember that you should put the debug points in even if you’re not currently debugging those parts – if and when you need to they will then be available, just by changing the DEBUG_LEVEL value.

In This Article


Tagged in ,