www.webdeveloper.com
Results 1 to 3 of 3

Thread: Number of queries executed?...

  1. #1
    Join Date
    Sep 2006
    Location
    Bucharest, RO
    Posts
    940

    Number of queries executed?...

    How would I be able to find out the number of queries executed in a script? I saw that many pages (especially forums) have in the footer "This page took xx.xx seconds to generate with xx queries". Is there a php function or mysql function that tells me this or do I have to manually count each query?...

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,321
    As far as I know, you'd have to count it yourself. I suppose you could make your own wrapper function that would both execute the query and increment a counter variable, which might save you a little typing in the long run.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2005
    Location
    Gold Coast, Australia
    Posts
    2,115
    I'd suggest either;
    Using a class and increment a private property:
    PHP Code:
    <?php

    class Query
    {
        
    /**
         * Number of queries counted
         * thus far
         */
        
    private $count 0;

        
    /**
         * Constructor
         *
         * @return void
         */
        
    public function __construct() {}
        
        
    /**
         * Execute Db query and
         * increment counter
         *
         * @param string $query
         * @param resource $link_identifier
         * @return resource
         */
        
    public function query($query$link_identifer NULL)
        {
            
    $this->incrementCounter();
            
            return 
    is_null($link_identifier) ? mysql_query($query) : mysql_query($query$link_identifier);
        }
        
        
    /**
         * Increment Query Counter
         *
         * @return void
         */
        
    private function incrementCounter()
        {
            
    $this->count++;
        }
        
        
    /**
         * Get Query Count
         *
         * @return int
         */
        
    public function getCount()
        {
            return 
    $this->count;
        }
    }


    /**
     * BRIEF EXPLANATION OF USE
     */
    $query = new Query;

    $result $query->query('CALL ALL OF YOUR QUERIES LIKE THIS');

    echo 
    'Num of queries: ' $query->getCount();
    Or using and incrementing a static variable within the function scope:
    PHP Code:
    function query($query$link_identifer NULL$return_count false)
    {
        static 
    $count;
        
        if (
    $return_count)
        {
            return 
    $count;
        }
        
        
    $count++;
        
        return 
    is_null($link_identifier) ? mysql_query($query) : mysql_query($query$link_identifier);
    }

    /**
     * BRIEF EXPLANATION OF USE
     */
    $result query('CALL ALL OF YOUR QUERIES LIKE THIS');

    echo 
    'Num of queries: ' query(00true); 

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles