Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-27-2013, 01:09 PM   PM User | #1
ROYW1000
Regular Coder

 
Join Date: Jan 2010
Posts: 151
Thanks: 10
Thanked 1 Time in 1 Post
ROYW1000 is an unknown quantity at this point
Queries Per Page and Query Time

Hi

I am looking for a script that will count the number of queries per page and also the query time however the script uses various methods like below to do queries and I don't want to change the several hundred types.

For example:

$result = $dbA->query("select * from $tableLabels");

$fieldList = $dbA->retrieveAllRecordsFromQuery("select * from $tableCustomerFields where type='AF' and visible=1 and internalOnly=0");

$cresult = $dbA->query("select * from $tableAffiliates where affiliateID=".$affiliateMain["affiliateID"]);

$dbA->query("update $tableAffiliates set arID=\"$rID\" where affiliateID=$jssAffiliateLogin");

Therefore is is possible to amend the below code so it can look at $dba as that would work without changing them all. Otherwise is it possible to find something else in common to make it work.

PHP Code:
<?php
//connexion
mysql_connect($host,$username,$password);
mysql_select_db('mydatabase');
//We start the counter at 0
$nb_queries 0;
//The function to time the execution time
function timer()
{
    
$time explode(' 'microtime());
    return 
$time[0]+$time[1];
}
$timer_queries 0;
//The fonction that count the number of queries and their execution time
function query($query)
{
        
//We increase the queries counter by 1
        
global $nb_queries,$timer_queries;
        
$nb_queries++;
        
//Execute the query and time the execution time
        
$beginning timer();
        
//Execute the query and save the result in a variable
        
$req_return mysql_query($query);
        
//We add the query duration to the total
        
$timer_queries += round(timer()-$beginning,6);
        return 
$req_return;
}

//We run some queries for the example
$requete1 query('select count(username) as nb from users');
$donnee1 mysql_fetch_array($requete1);
echo 
"The Data Base contain ".$donnee1['nb']." users.<br />";
$requete1 query('select username from users');
echo 
"This is a list of the users:<br />";
while(
$donnee1 mysql_fetch_array($requete1))
{
        echo 
"<strong>".$donnee1['username']."</strong><br />";
}

//Show the result
echo $nb_queries.' mysql queries executed in '.$timer_queries.' seconds';
?>
Thanks
Roy
ROYW1000 is offline   Reply With Quote
Old 01-30-2013, 07:12 AM   PM User | #2
jalex718
New to the CF scene

 
Join Date: Jan 2013
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
jalex718 is an unknown quantity at this point
I'd use something like this.

PHP Code:
<?php
//Usage
$dbC = new MysqlCounter();
$dbC->Query("SELECT * FROM `table");
echo 
$dbC->query_count." queries performed in ".$dbC->query_time." second(s)";


class 
MysqlCounter ()
{
    var 
$query_time 0;
    var 
$query_count 0;
    function 
query ($query$link_identifer false)
    {
        if(!
$link_identifer)
        {
            
$start_time microtime();
            
$result mysql_query($query);
            
$end_time microtime();
            
$query_time += ($end_time $start_time);
            
$query_count++;
        }
        else
        {
            
$start_time microtime();
            
$result mysql_query($query$link_identifer);
            
$end_time microtime();
            
$query_time += ($end_time $start_time);
            
$query_count++;    
        }
        return 
$result;
    }
}
?>
I also built in the feature to carry over the sql connection if needed.
jalex718 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:48 PM.


Advertisement
Log in to turn off these ads.