Counting MySql queries

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Linkjames
Forum Commoner
Posts: 90
Joined: Tue Sep 16, 2003 8:39 am

Counting MySql queries

Post 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
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post 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).
Last edited by McGruff on Wed Aug 10, 2005 5:20 pm, edited 1 time in total.
User avatar
phplancer.com
Forum Newbie
Posts: 2
Joined: Sun Sep 28, 2003 9:01 am
Location: Preston, UK

Re: Counting MySql queries

Post 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
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post 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...

?>
Post Reply