www.webdeveloper.com
Results 1 to 4 of 4

Thread: Calling query() function fails

  1. #1
    Join Date
    Jun 2005
    Posts
    201

    Calling query() function fails

    Hello. I am working with an example in a MySQL performance book, but it involves PHP. I cannot get the code example to work.

    It always reports:
    Fatal error: Call to a member function query() on a non-object in C:\wamp\www\page1.php on line 15

    Here is the code:
    PHP Code:
    <?php
    class mysqlx extends mysqli {
      function 
    query($query$resultmode) {
        
    $timer =  Timer::getInstance();
        
    $timer->startTime('MySQL');
        
    $res parent::query($query$resultmode);
        
    $timer->stopTime('MySQL'"Query: $query\n");
        return 
    $res;
      }
    }
      
    /*
     * Class DBQueryLog logs profiling data into the database
     */
        
        
    class DBQueryLog{
         
          function 
    logProfilingData($data) {
            
    $table_name "logs.performance_log_" . @date("ymd");
            
    $query "INSERT DELAYED INTO $table_name (ip, page, utime,
                   wtime, stime, mysql_time, sphinx_time, mysql_count_queries,
                   mysql_queries, user_agent, referer) VALUES  (.. data ..)"
    ;
            
    $res $this->mysqlx->query($query);
            
    // Handle "table not found" error - create new table for each new day
            
    if ((!$res) &&  ($this->mysqlx->errno == 1146)) { // 1146 is table not found
              
    $res $this->mysqlx->query(
                
    "CREATE TABLE $table_name LIKE logs.performance_log_template");
              
    $res $this->mysqlx->query($query);
            }
          }
          
            
    // This helper function implements the Singleton pattern
      
    function getInstance() {
        static 
    $instance;
        if(!isset(
    $instance)) {
          
    $instance = new Timer();
        }
        return(
    $instance);
      }
        }
        
    class 
    Timer extends DBQueryLog {
      private 
    $aTIMES = array();
      function 
    startTime($point)
        {
            
    $dat microtime()/time();
            
    $this->aTIMES[$point]['start'] = microtime(TRUE);
            
    $this->aTIMES[$point]['start_utime'] =
               
    $dat["ru_utime.tv_sec"]*1e6+$dat["ru_utime.tv_usec"];
            
    $this->aTIMES[$point]['start_stime'] =
               
    $dat["ru_stime.tv_sec"]*1e6+$dat["ru_stime.tv_usec"];
        }
      function 
    stopTime($point$comment='')
      {
        
    $dat microtime()/time();
        
    $this->aTIMES[$point]['end'] = microtime(TRUE);
        
    $this->aTIMES[$point]['end_utime'] =
           
    $dat["ru_utime.tv_sec"] * 1e6 $dat["ru_utime.tv_usec"];
        
    $this->aTIMES[$point]['end_stime'] =
           
    $dat["ru_stime.tv_sec"] * 1e6 $dat["ru_stime.tv_usec"];
        
    $this->aTIMES[$point]['comment'] .= $comment;
        
    $this->aTIMES[$point]['sum'] +=
           
    $this->aTIMES[$point]['end'] - $this->aTIMES[$point]['start'];
        
    $this->aTIMES[$point]['sum_utime'] +=
           (
    $this->aTIMES[$point]['end_utime'] -
              
    $this->aTIMES[$point]['start_utime']) / 1e6;
        
    $this->aTIMES[$point]['sum_stime'] +=
           (
    $this->aTIMES[$point]['end_stime'] -
              
    $this->aTIMES[$point]['start_stime']) / 1e6;
      }
      function 
    logdata() {
        
    $query_logger DBQueryLog::getInstance('DBQueryLog');
        
    $data['utime'] = $this->aTIMES['Page']['sum_utime'];
        
    $data['wtime'] = $this->aTIMES['Page']['sum'];
        
    $data['stime'] = $this->aTIMES['Page']['sum_stime'];
        
    $data['mysql_time'] = $this->aTIMES['MySQL']['sum'];
        
    $data['mysql_count_queries'] = $this->aTIMES['MySQL']['cnt'];
        
    $data['mysql_queries'] = $this->aTIMES['MySQL']['comment'];
        
    $data['sphinx_time'] = $this->aTIMES['Sphinx']['sum'];
        
    $query_logger->logProfilingData($data);
      }
      

    }

    // Start of the page execution
    mysql_connect("localhost","root","");
    $timer =  Timer::getInstance();
    $timer->startTime('Page');
        
        
    // $query = "SELECT * FROM sakila";
        // mysql_query($query,$db);
    // End of the page execution
    $timer->stopTime('Page');
    $timer->logdata();
    ?>
    Barkleigh's Webmaster

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,921
    As currently coded, there is no instance of your mysqlx class in the DBQueryLog class.
    "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
    Jun 2005
    Posts
    201
    When I try to create the a class instance in the DBQueryLog() class like so: mysqlx = new mysqlx();

    I receive this error:
    Parse error: parse error, expecting `T_FUNCTION' in C:\wamp\www\page1.php on line 16
    Barkleigh's Webmaster

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,921
    How/where are you instantiating it?

    You may want a __construct() method in the class to instantiate it:
    PHP Code:
    class ClassName
    {
       protected 
    $mysqlx;
       public function 
    __construct()
       {
          
    $this->mysqlx = new mysqlx();
       }
       
    // rest of class...

    However, since mysqlx extends MySQLi, which normally gets the DB connection credentials passed to its constructor, you might be better off instantiating it in your main script, then passing that instance as a parameter to the constructor:
    PHP Code:
    class ClassName
    {
       protected 
    $mysqlx;
       public function 
    __construct(mysqlx $mysqlx)
       {
          
    $this->mysqlx $mysqlx;
       }
       
    // rest of class...
    }
    $mysqlx = new mysqlx($dbhost$dbuser$dbpass$dbname);
    $foo = new ClassName($mysqlx); 
    "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

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