PHP5 to watch over MySql queries

If the developer wants to write more efficient MySql queries, he may want to display all the queries a web page has generated and the time the whole script took to output its Html result. This is useful while developing. Once the website is launched, you may get rid of these script lines (just comment them with // or /* */) in order not to take extra CPU time.

Changes in the MySql class

In order to count MySql queries generated by a web page and display the list of them, he should add a static variable to his MySql Class :

public static $queries = array();

When initializing the class, the static variable $queries becomes an empty array. Each time a query will be sent to the class for execution, array $queries will be incremented this way :
Before destroying the $mysql object
return count(self::$queries);
will return the number of MySql queries generated by a Web page.
return self::$queries;
will return the array containing all MySql queries for the current web page.

PHP script execution time

In order to check how long it takes a script from start to end, just work with microtime :
//top of PHP script
$start_time = microtime(true); // >= PHP 5.0.0 gives microtime as a float
//bottom of PHP script
$end_time = microtime(true);
$total_time = $end_time – $start_time;

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>