Jump to content

How do I "count" queries in PHP (MySQL)


SNN

Recommended Posts

I am trying to find the function for PHP where you can get the number of mysql queries and print them on a webpage.

I know this is possible, I've seen this on a few forums...how can I do this?

"Page generated in 2.29798 seconds with 16 queries"

 

Thanks,

Billy

Link to comment
Share on other sites

Just off the top of my head, you could write a function like this:

 

function query_and_count($query) {
   global $count;      
   $count = $count + 1;
   return mysql_query($query);
}

 

Then, at the top of your scripts, add this code:

 

global $count = 0;
$start_time = time();

 

And each time you want to do a query, you just call the code (for example):

 

query_and_count("SELECT * FROM table);

 

And at the end of your script, you use this code:

 

$finish_time = time();
$total_time = $finish_time - $start_time;
$output_time = date("s", $total_time) . "." . date("u", $total_time);
echo "Page generated in " . $output_time . " seconds with " . $count . "queries";

 

disclaimer: non-tested, just written off the top of my head.

Link to comment
Share on other sites

Ah, my friend suggested this as well, but by just adding $qc++; at the end of each mysql_query.

I thought I saw a PHP function to do this before but this will work just as well (Should work from the looks of it.)

 

Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.