PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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