SNN Posted March 2, 2008 Share Posted March 2, 2008 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 https://forums.phpfreaks.com/topic/93937-how-do-i-count-queries-in-php-mysql/ Share on other sites More sharing options...
haku Posted March 2, 2008 Share Posted March 2, 2008 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 https://forums.phpfreaks.com/topic/93937-how-do-i-count-queries-in-php-mysql/#findComment-481322 Share on other sites More sharing options...
SNN Posted March 2, 2008 Author Share Posted March 2, 2008 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 https://forums.phpfreaks.com/topic/93937-how-do-i-count-queries-in-php-mysql/#findComment-481332 Share on other sites More sharing options...
haku Posted March 2, 2008 Share Posted March 2, 2008 You could use $count ++; and it would work fine. Shorter code too which is good. Link to comment https://forums.phpfreaks.com/topic/93937-how-do-i-count-queries-in-php-mysql/#findComment-481406 Share on other sites More sharing options...
SNN Posted March 2, 2008 Author Share Posted March 2, 2008 Very true, but this is for a custom forum software, so extra functions for debugging mode will be good. Thanks everyone. Billy Link to comment https://forums.phpfreaks.com/topic/93937-how-do-i-count-queries-in-php-mysql/#findComment-481472 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.