vForums Support > Programming & Coding :: Programming Discussion :: > I'll try this again... How to get the total quries

I'll try this again... How to get the total quries - Posted By Dylan (dylan) on 20th Jan 08 at 2:06am
Ok since cr0w just had to mess up my thread {Sad} i'll just try again...

so I just want to count all the php/mysql queries on my own php scripts. {Wink} I'm wondering how Ross did it.



And this it to wrightys reply in my old thread.

I wasn't asking on here, I'm almost positive you can't count the total queries with any client side script. This is for my own php and mysql project {Smile}

At cr0w's reply.

I did post in the programming board didn't I ?





Re: I'll try this again... How to get the total .. - Posted By Michael (wrighty) on 20th Jan 08 at 2:16am
ahh ok...

by you saying that proves cr0w was right.. {Tongue Out}




I would say... run a query and when the query is ran a value is incremented by one... then when you have ran all of the queries you simply output that number... {Unsure}

Cr0w or Ross can correct me if that is an epic phail! {Tongue Out} Try that though! {Cheesy}

Re: I'll try this again... How to get the total .. - Posted By Marc (cr0w) on 20th Jan 08 at 3:13am
Yeah Wrighty, that was pretty close. {Smile}

I usually do this:

Code:
 
  1. //Top of page
  2. $queries = 0;
  3. $start = microtime();
  4. function query($s){
  5.     $queries++;
  6.     return mysql_query($s);
  7. }
  8.  
  9. /* Page Content Here */
  10.  
  11. //Bottom of page
  12. $end = microtime();
  13. echo 'Page generated in '.$end-$start.' seconds, with '.$queries.' queries.';
 

Re: I'll try this again... How to get the total .. - Posted By Dylan (dylan) on 20th Jan 08 at 4:29am
i don't understand your query function crow?

Re: I'll try this again... How to get the total .. - Posted By Ross (admin) on 20th Jan 08 at 11:33am
 
i don't understand your query function crow?


The way Cr0w said is very similar to what I use except my query function is a little more advanced since it does error logging, replaces special values in the query string and can return the query result. But the basic idea of it is so you would call

$myquery = query("SELECT * FROM table_name WHERE field='value' LIMIT 1");

Which would then use the query() function to increase the # of queries used as well as running the actual query.

Re: I'll try this again... How to get the total .. - Posted By Marc (cr0w) on 20th Jan 08 at 3:07pm
 
 
i don't understand your query function crow?


The way Cr0w said is very similar to what I use except my query function is a little more advanced since it does error logging, replaces special values in the query string and can return the query result. But the basic idea of it is so you would call

$myquery = query("SELECT * FROM table_name WHERE field='value' LIMIT 1");

Which would then use the query() function to increase the # of queries used as well as running the actual query.


I do almost the same thing, Ross. {Smile} I update my query log & error log, as well as return an array [optional].

Usually something like this:
Code:
 
  1. function query($s,$a="false"){
  2.  
  3.     //Update # Queries
  4.     $queries++;
  5.  
  6.     //Query Log
  7.     error_log("Query: ".$s." @ ".date(),3,"query_log.txt");
  8.  
  9.     //Run Query
  10.     $query = mysql_query($s) or die(error_log("Query Error: ".mysql_error()." @ ".date(),3,"error_log.txt");
  11.  
  12.     //Create Array
  13.     if($a=="assoc"){
  14.         $query = mysql_fetch_assoc($query);
  15.     }
  16.     else if($a=="array"){
  17.         $query = mysql_fetch_array($query);
  18.     }
  19.     else if($a=="numrows"){
  20.         $query = mysql_num_rows($query);
  21.     }
  22.  
  23.     //Return Query
  24.     return $query;
  25. }
 


I was just showing Dylan how to show then number of queries, as well as the time to load the page. {Wink}