PHP number of queries executed

Discussion in 'Programming/Scripts' started by Juddling, Jul 26, 2007.

  1. Juddling

    Juddling New Member

    Is there a way of counting the number of queries executed I have seen it on other websites.

    Changing all my mysql_query() functions to a "home-made" function is out of the question, I have thousands of uses of the function.

    Is this possible?

    Thanks
     
  2. till

    till Super Moderator Staff Member ISPConfig Developer

    Have a look at the function:

    mysql_affected_rows()

    to get the number of records affected by your query.
     
  3. Juddling

    Juddling New Member

    mysql_affected_rows()

    That's only for your last query executed and it still doesn't do what i'm looking for sorry mate.
     
  4. till

    till Super Moderator Staff Member ISPConfig Developer

    The function must be called after each execution of the mysql_query command. There is no other solution, thats the way it is done in PHP and other sites are doing it this way.
     
  5. blu3ness

    blu3ness New Member

    We do it by writing a wrapper class to wrap all of our MYSQL query functions.
    It makes it very ez to tally these kind of statistics if it is ever required.

    So yes, I would suggest writing your own wrapper functions for those mysql queries, they will come in very handy later on
     
  6. smeguru

    smeguru New Member

    Wrapper example

    Would you mind sharing the code for your wrapper as this is something I would like to do on a new project myself.


     
  7. blu3ness

    blu3ness New Member

    This is a part of the wrapper. Just to give you an example :) do not toe xpect it to be in working condition..

    PHP:
    class MysqlDataBase implements DataBase
    {
        
    /* === Constructor / Destructor === */
        
    function __construct $host$username$passwd )
        {
        
    $this->link null;
        if( !( 
    $this->link mysql_pconnect$host$username$passwd ) ) )
            
    // Connection could not be opened.
            
    throw new DataBaseExceptionmysql_error(), ERR_DB );
        }

        function 
    __destruct()
        {
        if( 
    $this->link )
            
    mysql_close$this->link );
        }

        
    // isConnected:
        // Returns true if the database is currently connected.
        
    public function isConnected()
        {
        return 
    mysql_ping$this->link );
        }

        
    /* === DataBase === */

        // useDataBase:
        // Selects 'db' for use in proceeding queries
        
    public function useDataBase$db )
        {
        if( !
    mysql_select_db$db$this->link ) )
            throw new 
    DataBaseExceptionmysql_error(), ERR_DB );
        }

        
    /* === Transaction Stuff === */

        // commitTransaction:
        // Commits all of the queries made since startTransaction was
        // called.
     
        // startTransaction:
        // Start a transaction.
     

        // rollBackTransaction:
        // Cancels the current transaction.
     
        // setSavePoint:
        // Creates a new save point and returns an object of type SavePoint
      

        // releaseSavePoint:
        // Releases the given save point
     
        // rollBackToSavePoint:
        // Roll's back the transaction to the given save point
     
        /* === Run Query === */

        // query:
        // Run a query then return an object of type QueryResult
        
    public function query$query )
        {
        
    $this->count++;

        if( 
    $this->do_print )
            print 
    $query."<br><br>";

        if( 
    $result mysql_query$query$this->link ) )
        {
            
    // Query suceeded create a new wrapper class from $result
            
    return new MysqlQueryResult$result );
        }else{
            throw new 
    DataBaseExceptionmysql_error()."<br><br>Query: $query"
                         
    ERR_DB );
        }
        }

        
    // getAffectedRowCount:
        // Returns the number of rows affected by the last operation.
        
    public function getAffectedRowCount()
        {
        return 
    mysql_affected_rows$this->link );
        }

        
    // getLastAutoID:
        // Returns the last generated auto_increment id
        
    public function getLastAutoID()
        {
        return 
    mysql_insert_id$this->link );
        }

        
    /* === Error === */

        // getLastError:
        // Returns a string containing the last error that occured.
        
    public function getLastError()
        {
        return 
    mysql_error($this->link);
        }

        
    // getLastErrorNum:
        // Returns a string containing the last error number that occured.
        
    public function getLastErrorNum()
        {
        return 
    mysql_errno($this->link);
        }

        
    /* === Info === */
        
        // getQueryCount:
        // Returns the total number of queries made to the database
        
    public function getQueryCount()
        {
        return 
    $this->count;
        }

        
    // setPrintQueries:
        // Sets wether or not queries should be printed.
        
    public function setPrintQueries$do_print )
        {
        
    $this->do_print $do_print;
        }



        
    /* === Private Data === */

        
    private static $save_point_id;

        
    // Database Link
        
    private $link;
        private 
    $do_print;
        private 
    $count;
    }




    ?>
     

Share This Page