# PHP Quick Profiler README A fork of the PHP Quick Profiler done by [Particle Tree](http://particletree.com/features/php-quick-profiler/). Blog post describing this in further detail is available cached at [Cloudfront](http://d3rxqy8m5km8r7.cloudfront.net/features/php-quick-profiler/) ### Screenshots * [Screenshot 1 - PQP on your webpage](http://farm4.static.flickr.com/3625/3463457814_0a558911e9_o.png "Screenshot 1 - PQP on your webpage") * [Screenshot 2 - PQP Detail](http://farm4.static.flickr.com/3557/3465935161_9cac2d37f6_o.png "Screenshot 2 - PQP Detail") * [Screenshot 3 - Memory Used](http://farm4.static.flickr.com/3615/3463780326_bfffe5cb13_o.png "Screenshot 3 - Memory Used") * [Screenshot 4 - Files Included](http://farm4.static.flickr.com/3555/3466762904_45da31d7d1_o.png "Screenshot 4 - Files Included") * [Screenshot 5 - Load Time](http://farm4.static.flickr.com/3489/3463039665_7c65f67535_o.png "Screenshot 5 - Load Times") * [Screenshot 6 - SQL Query Times/Auto Explain](http://farm4.static.flickr.com/3633/3463902088_5d9738164c_o.png "Screenshot 6 - Query Times/Auto Explain") ### On This Page * Introduction and Overview of Files * Getting the Example Working * Setting up the Database Class * Using Smarty instead of PHP echos ### Introduction and Overview of Files PHP Quick Profiler is a helper class that outputs debugging related information to the screen when the page has finished executing. This zip package contains a functional example project that utilizes the helper classes. - index.php : The landing page of the example. Navigate to it in your browser to see the demo. - display.php : Contains the markup for PQP. - pqp.tpl : A Smarty variation of the PQP markup. - /css/ : The stylesheets used by PQP. - /images/ : The images used by PQP. - /classes/Console.php : The class used to log items to the PQP display. - /classes/MySqlDatabase : A sample database wrapper to explain how database logging could be implemented. - /classes/PhpQuickProfiler : The core class that compiles the data before outputting to the browser. ### Getting the Example Working For the most part, the example will work once you drop it in your root directory. There are a few settings to check though. - In PHPQuickProfiler.php, set the $config member variable to the path relative to your root (located in the constructor). - If PQP does not appear after navigating to index.php in your browser, locate the destructor of the PQPExample class (at the bottom). Rename the function from __destruct() to display(). Then, manually call the function display() just underneath the class after the call to init(). The reason this would happen is because the destructor is not firing on your server configuration. - At this point, everything should work except for the database tab. ## Setting up the Database Class *NOTE* - This step does require knowledge on PHP / Database interactions. There is no copy/paste solution. Logging database data is by far the hardest part of integrating PQP into your own project. It requires that you have some sort of database wrapper around your code. If you do, it should be easy to implement. To show you how it works, follow these steps with the sample database class we have provided. Create a database named 'test' and run the following query on it: CREATE TABLE `Posts` ( `PostId` int(11) unsigned NOT NULL default '0', PRIMARY KEY (`PostId`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 * In `index.php`, uncomment out the second include, which includes the database class. * In `index.php`, uncomment out the `function sampleDatabaseData()`. * In the `sampleDatabaseData()`, supply your database host, username, password, and database name. Given those steps, database logging will be enabled. If you would like to transition this to your own database class, open `/classes/MySqlDatabase.php` and note the following: * `$queryCount` and `$queries` member variables declared on initialization * When a query is run, the following is executed: Code: <?php $start = $this->getTime(); $rs = mysql_query($sql, $this->conn); $this->queryCount += 1; $this->logQuery($sql, $start); ?> - Everything in `/classes/MySqlDatabase.php` under the section comment "Debugging" must be available for the above snippet to work. ### Using Smarty instead of PHP echos We love Smarty and hate echos, but to make this work for everyone we set the default as echos. To show love to the Smarty users out there, we have included a pqp.tpl file for PQP. To make it work, you would have to change the following in /classes/PhpQuickProfiler.php: - Add a require_once to your Smarty Library. - In the constructor, declare an instance of Smarty: `$this->smarty = new Smarty(...);` - Everywhere in in the code you see $this->output[... change it to a smarty assign. For example: <?php $this->output['logs'] = $logs; ?> ... becomes ... <?php $this->smarty->assign('logs', $logs); ?> After doing it once, you'll see the pattern and can probably use a find/replace to do the rest quickly. - Locate the `display()` function at the bottom. Remove the last 2 lines, and add: `$this->smarty->display('pathToDisplay.tpl');` All set after that!