JustinK101 Posted June 22, 2007 Share Posted June 22, 2007 Is there a way to get the time in seconds the previously executed mysql query took from php? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/ Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 this really isn't but surrond your query with this: <?php // Function to calculate script execution time. function microtime_float () { list ($msec, $sec) = explode(' ', microtime()); $microtime = (float)$msec + (float)$sec; return $microtime; } // Get starting time. $start = microtime_float(); /* Query Here */ $end = microtime_float(); // Print results. echo '<br/><br/>Search Execution Time: ' . round($end - $start, 3) . ' seconds<br/><br/>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279901 Share on other sites More sharing options...
JustinK101 Posted June 22, 2007 Author Share Posted June 22, 2007 Ok I see, but I dont want to insert this code into my project which is well over 50 php pages, and find all the queries. I was hoping there was just a way to get the time the previous query took? That way, I can just stick the value into my footer include file. I know phpMyAdmin displays the time the previous query took, do they do it this way? Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279905 Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 I was just going to say phpmyadmin does it somehow, maybe you can find it in there. You really just need to say $start before and $end after but yeah there might be a way Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279907 Share on other sites More sharing options...
JustinK101 Posted June 22, 2007 Author Share Posted June 22, 2007 How about a way to count the total number of queries executed for a particular page load? I know Simple Machine Forum has the ability to do this. Theirs displays: Page created in 0.751 seconds with 11 queries. Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279911 Share on other sites More sharing options...
cooldude832 Posted June 22, 2007 Share Posted June 22, 2007 it probably just counts an array of queries and uses the same script. Php isn't big on times Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279912 Share on other sites More sharing options...
btherl Posted June 22, 2007 Share Posted June 22, 2007 You can create a wrapper for mysql_query, and then do a search and replace over all your files (and make sure to require() it in each file too). Then have the wrapper record timing information. Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279931 Share on other sites More sharing options...
JustinK101 Posted June 22, 2007 Author Share Posted June 22, 2007 btherl: Can you explain what you mean wrapper? Can I overide mysql_query(), i.e. create my own function called mysql_query()that gets called? Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279956 Share on other sites More sharing options...
btherl Posted June 22, 2007 Share Posted June 22, 2007 The short answer is no, you can't override functions (well you can, but I wouldn't recommend it). What you can do is make your own "wrapper" function: function my_mysql_query($query) { # Do timing stuff $res = mysql_query($query); # Do timing stuff return $res; } And then replace every call to mysql_query() with my_mysql_query(). You can even do fancy stuff like store time for each individual query in an array, and then dump that array if a debugging flag is set. It's great for seeing which queries are eating up your time. It's called a wrapper function because it wraps around the real function, doing a bit of extra stuff. Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279961 Share on other sites More sharing options...
JustinK101 Posted June 22, 2007 Author Share Posted June 22, 2007 btherl, Ok that makes sense. I think I just want to do total queries executed per page instead of time, makes more sense, since the time would just be the last executed query. How would I keep track of that. For example I have a header.php file which calls a query, then in the page there might be three queries, then in the footer there is another query. I would want 4 retutrned in this case. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279962 Share on other sites More sharing options...
JustinK101 Posted June 22, 2007 Author Share Posted June 22, 2007 Ok, here is what I have to track the number of queries per page: //Tracks The Number Of Queries Per Page. global $numb_queries; $numb_queries = 0; function smart_mysql_query($sql) { $numb_queries++; echo "<script>alert('" . $numb_queries . "');</script>"; return (mysql_query($sql)); } The echo and JS alert is just to help me to debug this thing. For some reason the alert keeps on popuping up 1, no matter how many queries it runs, then in the page the value of numb_queries is 0. Any ideas? I don't get what is gonig on. Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279975 Share on other sites More sharing options...
JustinK101 Posted June 22, 2007 Author Share Posted June 22, 2007 OHh I got it. Duhhhh! //Tracks The Number Of Queries Per Page. $numb_queries = 0; //Mysql_query() Wrapper Function. function smart_mysql_query($sql) { global $numb_queries; $numb_queries++; return (mysql_query($sql)); } Hopefully this helps somebody else, this seems to work well, simply echo out the value of $numb_queries to get the number of queries used on a page. Quote Link to comment https://forums.phpfreaks.com/topic/56665-time-previous-mysql-query-took-to-execute/#findComment-279976 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.