Okay, this is one of the most difficult things to do in any event driven, web development environment. And it’s no different in PHPRunner. Most of the event code we’re playing with is PHP code. The snag is, you can’t just stick an “alert” in any old event line and have it appear on the screen! javascript is interfering with things… PHP code is on the server not the browser, flow of control has to happen before that alert gets back to the browser, lots of things basically get in the way of tracing what’s really happening in your front end and back end code.
The simplest way I found to manage this is to use a table to trace things. I created a simple table (MySQL – adapt for other dialects!) called “debug”.
CREATE TABLE IF NOT EXISTS debug ( `id` int(11) NOT NULL AUTO_INCREMENT, `debug_msg` text, `create_dt` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1 ;
There. Pretty easy. Now how to use this in an easy fashion? Remember that article I did about using PHP external files, and including them in your PHPRunner applications?
That’s what we’re going to do. We’ll write a very simple function in PHP to insert a trace message into the debug table.
Here’s what that function looks like:
<?php /* debugging and utility functions */ function fpi_debug($conn, $arg_error_text){ $stamp = date('Y-m-d H:i:s'); $strSQLInsert = "insert into debug ( debug_msg, create_dt ) values ( "; $slashed_text = addslashes($arg_error_text); $strSQLInsert = $strSQLInsert . "'" . $slashed_text . "', '" . $stamp . "')"; CustomQuery($strSQLInsert); } ?>
Notice that I put a call to addslashes() in here too. Very often when I am doing asserts, the thing I need to verify is that a SQL statement has been assembled correctly including all double-quotes and single-quotes, so addslashes() takes care of making the SQL statement safe to insert into a database column as a text string.
Note also that the function gets passed the connection object from PHPRunner, so this is now a convenient extension of the PHP event code.
I named my file “funcs1.php” and saved it into the PHPRunnerProjects\myApp\source\ folder. That way PHPRunner knows where to find it.
You should put the include statement in the After_Application_Initialized event of your application. That would make all the functions in it globally available to all pages and all event code in your application.
include "funcs1.php";
that’s it for the setup.
Now in any event script that you want to use the function you need to do two things.
1. ensure that you have declared global access to the $conn object.
2. make a call to the function
a function call would look something like this:-
obviously this is an example from my application code, but you’ll get the idea! this is doing a custom add.
global $conn; ... ... ... // insert a blank new row for this subscriber $sqlstring = "insert into choose_plans (subscriber_id )" . " VALUES ( " . $_SESSION["current_subscriber_id"] . ")"; CustomQuery($sqlstring); // now push that sqlstring construct out to the debug table to see what actually got constructed!! fpi_debug($conn, $sqlstring); ... ... ...
That’s pretty much it. In some applications I’ve tweaked this a bit to have the fpi_debug function only do an insert if a session variable is set to some value, so that I can leave the trace statements in the code even in production. But that’s up to you. Most of the time when I want this capability, I want it for a one-time verify of how something is firing and in which sequence so I can test my assumptions. And once I’ve found the culprit, I will remove the debug ‘assert’ statements. But that’s entirely up to you. Now you have the tools, you can finesse this in lots of ways.
And as a final note, the reason I prefixed my function names “fpi_” is because I’m paranoid! rather than just f_debug, I wanted to be reasonably sure that my function name could not possibly clash with anything else in any other code component. P and I are my initials 🙂 You can name your generic support utility functions anything you like.