Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP show time taken to retreive MYSQL query

Status
Not open for further replies.

scriggs

IS-IT--Management
Jun 1, 2004
286
GB
I have been to some websites that show at the bottom of the page how long operations take.

I have a PHP page which extracts data from a MYSQL database and then uses JPGraph to create a graphs.

How can I time the operation and print on bottom of page?

Also, is there any way to make a progress bar, or spinning icon, etc. so that the user doesn't get impatient...
 
i fear there is no neat php function nor mysql return that gives you this information.

phpmyadmin does the following:

Code:
list($usec, $sec) = explode(' ',microtime());
$querytime_before = ((float)$usec + (float)$sec);
$result   = //perform query
list($usec, $sec) = explode(' ',microtime());
$querytime_after = ((float)$usec + (float)$sec);
$querytime = $querytime_after - $querytime_before;

i think this introduces some minor latency in parsing the explode directive. for something even preciser you might do the following (i.e. all the processing done outside of the timing points):
Code:
$querytimebefore = microtime();
$result   = //perform query
$querytimeafter = microtime();
list($usec, $sec) = explode(' ',$querytimebefore);
$querytimebefore = ((float)$usec + (float)$sec);
list($usec, $sec) = explode(' ',$querytimeafter);
$querytimeafter = ((float)$usec + (float)$sec);
$querytime = $querytimeafter - $querytimebefore;
 
Thanks jpadie

I was already running a simple $querytimebefore and after, just wondered if there was a better way to do it. My main plan was to try and identiy exactly where time was being taken, but will stick with the simple then.

Cheers.
 
i believe that there is another way to do this, depending on how you have set up your mysql server.

the query time is stored in the general query log. you need to open the log (use fopen and fread) and parse it for the query time.

i have read about this but never had the need or inclination to do it myself. the method came from none other than Rasmus Lerdorf.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top