Page 1 of 1

Counting MySql queries

Posted: Sat Sep 27, 2003 9:11 am
by Linkjames
I am trying to add a peice to my page that shows the time taken to execute the script and the MySql queries used. I have the time taken script, but have no idea where to start on the querys part. Any ideas or links?

cheers guys - LinkJames

Posted: Sat Sep 27, 2003 10:04 am
by McGruff
You could create a custom function along the lines of:

Code: Select all

<?php
function dbQuery($sql, &$log) {

    echo $sql . '<br />';
    $query = mysql_query($sql) or die(mysql_error() . '<br />');
    return $query;
}
?>
Wrapping up the native db functions basically provides a socket where you can add additional functionality and then change these attached behaviours at will.

Sockets can be very useful while developing. You can comment the above echo line on and off with a quick edit.

Or, with a single edit of dbQuery(), you can quickly change the error reporting level for a live site or local development.

Another possibility: extending this idea with wrappers for all the mysql_ fns could create a simple db access layer. A few quick edits would allow you to switch to a new type of database (assuming you have been careful not to use any db specific query statements).

Re: Counting MySql queries

Posted: Sun Sep 28, 2003 9:17 am
by phplancer.com
For the timing part use the microtime() php function.
http://php.planetmirror.com/manual/en/f ... rotime.php
You'll find some good code in the user comments.
Cheers! :lol:

Linkjames wrote:I am trying to add a peice to my page that shows the time taken to execute the script and the MySql queries used. I have the time taken script, but have no idea where to start on the querys part. Any ideas or links?

cheers guys - LinkJames

Posted: Sun Sep 28, 2003 11:18 am
by Cruzado_Mainfrm
first, u have to make single connection to count how many queries in that single connection to the db, if you have more connections, just add em' up:

Code: Select all

<?php
//1.connect to mysql

//2.set a variable to 0, ex: $i=0;

//3.make a query, and after the query, increase variable $i, ex: $i++;

//4.repeat step 3 until necessary

//now the counting part
echo $i //will show the number of queries...

?>