Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    160
    Thanks
    10
    Thanked 1 Time in 1 Post

    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

  • #2
    New to the CF scene
    Join Date
    Jan 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •