It has been a little while since I made an entry, but there is good reason for that. I have been hard at work redesigning my Gallery layout for my picture galleries on My Air Force Life. The main thing I have been pushing for was integration into MySQL. There are obvious benefits of switching to that rather than just having static pages, but that is not the point of this entry.

The reason for this entry is to show how I did the little counter at the bottom of the pages. You know the ones…

Page created in x.xxx seconds with X queries.

Well I was wanting to figure out how to get this to work on my site. Part of the reason, is I am using my laptop as a testbed, so I want to see how resource intensive some of my queries may be. This is my first time actually developing something with MySQL, rather than editing existing code, and I wasn’t sure that my queries were going to be all that resource friendly.

Well to start since I will be doing many different queries on many different pages, I felt that I needed to create a function in a separate file that I would just include on my other files. I called the file func.query_time.php

Within that file, I had two separate functions. One which will time the query, add it to the total time, and add the amount of queries. The second function is just to display the actual info at the bottom of the pages. I will go through these functions separately, so people understand how they work. But I will have everything all together at the bottom.

So to start out we need to declare the function and we are calling it query.

function query($sql, $querycount, $totaltime)
{
   if (empty($querycount))
      $querycount=0;
 
   if (empty($totaltime))
      $totaltime=0;

All this does is recieve the query ($sql) and the querycount and totaltime amounts. And if the latter two were not passed when the function was called then it automatically sets them at zero. Next we need to get the time it takes to process the query.

   list($usec, $sec) = explode(' ',microtime());
   $querytime_before = ((float)$usec + (float)$sec);
 
   // run the query
   $result = mysql_query($sql);
 
   list($usec, $sec) = explode(' ',microtime());
   $querytime_after = ((float)$usec + (float)$sec);

The first part grabs the time before the query was run and stores it in $querytime_before. Then we actually run the MySQL query, then we get the time again and store it in $querytime_after. Now we need to get the totals of everything.

   $querytime = $querytime_after - $querytime_before;
   $totaltime += $querytime;
 
   $querycount++;

The first line take the time before and subtracts it from the time after which gives us the time it takes to make that query. Then we add the $querytime to $totaltime (the += is for adding another variable to the one it will be stored in ie. $totaltime = $totaltime + $querytime;). The last line is to add 1 to the querycount. The final part of this function is to return the info so you can use it on your regular page.

   return array($result, $querycount, $totaltime);
}

This returns the results back into the orginal file and closes out the function. Now onto the file itself and how you call the query.

Now the first time you call the function “query” there will be no variables set for $querycount or $totaltime, so you can just leave those out, but those variables will be returned. And since they are returned in an array there are a couple of ways to get them out. I prefer the list() function as it is simple and you can name the variables however you want.

   "sql = SELECT * FROM database";
   list($result, $querycount, $totaltime) = query($sql);

So the $sql query we have stored above is being passed to the function on the right, and then it runs through the function and returns us our info. On the second query it is only slightly different.

   $sql = "SELECT * FROM database WHERE id = 1";
   list($result2, $querycount, $totaltime) = query($sql, $querycount, $totaltime);

In this one we need to pass the $querycount and the $totaltime to the function. Otherwise it won’t know the numbers. Also note that you should change the variable for the MySQL query result depending on how your page is setup.

Now we have the displaying of the info. In the func.query_time.php file I had a second function declared. This function is small enough I won’t break it down.

function display_time($querycount, $totaltime)
{
   $strQueryTime = 'Page created in %01.4f seconds';
   echo '<p class="querytime">' . sprintf($strQueryTime, $totaltime) . ' with ' . $querycount . ' queries.</p>
}

So the function gets the querycount and totaltime variables passed to it. Then we store the string for the page created in x seconds into $strQueryTime. The %01.4f is used to shorten up the totaltime. Right now it will pad any unused spaces with 0′s and will go four decimal places to the right. We then use the sprintf function to display that with the totaltime added in and then the total number of queries that were counted. All that is left to do is show that on the page. This is done with

display_time ($querycount, $totaltime);

Now to show it all together. First the php file followed by the function file.

<?php
  require_once('func.query_time.php');
 
  $sql="SELECT * FROM database";
  list($result, $querycount, $totaltime)=query($sql);
 
  $sql="SELECT * FROM database WHERE id='1'";
  list($result2, $querycount, $totaltime)=query($sql, $querycount, $totaltime);
 
  display_time($querycount, $totaltime);
?>
<?php
 
function query($sql, $querycount, $totaltime)
{
 
  if(empty($querycount))
    $querycount=0;
 
  if(empty($totaltime))
    $totaltime=0;
 
  list($usec, $sec) = explode(' ',microtime());
  $querytime_before = ((float)$usec + (float)$sec);
 
  $result=mysql_query($sql);
 
  list($usec, $sec) = explode(' ',microtime());
  $querytime_after = ((float)$usec + (float)$sec);
 
  $querytime = $querytime_after - $querytime_before;
 
  $totaltime += $querytime;
 
  $querycount++;
 
  return array($result, $querycount, $totaltime);
}
 
function display_time($querycount, $totaltime)
{
 
  $strQueryTime = 'Page created in %01.4f seconds';
 
  echo '<p class="querytime">' . sprintf($strQueryTime, $totaltime) . ' with ' . $querycount . ' queries.</p>';
}
 
?>

So there you have it. Any questions or things you believe I could do better, let me know in the comments. And I am sure that there will be more code that I will post before the gallery overhaul is complete. Stay tuned…

9 Comments

  1. Php Explode String says:

    I found your site on faves.com bookmarking site.. I like it ..gave it a fave for you..ill be checking back later

  2. John Booxfreeral says:

    First of all congratulation for such a great site. I learned a lot reading article here today. I will make sure i visit this site once a day so i can learn more.

  3. xanaxo says:

    Your are Great. And so is your site! Awesome content. Good job guys!

  4. awesome videos says:

    thanks for the function, comes in handy for debugging!

  5. Jay Perez says:

    Hi there,

    Where do i insert my query? sorry im new to php and i find this very good.

    Thank you,

    Jay

  6. Jay Perez says:

    I get this error:

    Warning: Missing argument 2 for query(), called in C:\xampp\htdocs\www\production\query_counter.php on line 6 and defined in C:\xampp\htdocs\www\production\func.query_time.php on line 3

    Warning: Missing argument 3 for query(), called in C:\xampp\htdocs\www\production\query_counter.php on line 6 and defined in C:\xampp\htdocs\www\production\func.query_time.php on line 3

  7. Jeremy says:

    After the first query($sql) you need to pass the $querycount and $totaltime variables back to the function.

    Without seeing your code, I would guess that is your problem.

  8. F7 says:

    Hi Jay Perez,

    Change First Part To…

     

    This should fix your issue.

  9. Rerenwo says:

    This function would be improved a lot with the use of globals, thus not needing to pass the count and time as parameters to it.

    Though; thank you for the guide :) It’s well explained and easy to follow ;)

Leave a Reply

XHTML: To type code in the comments use <pre lang=""> </pre>. Supported languages can be found here.