Quick and dirty MySQLi wrapper for query logging

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

I wanted a list of queries executed during page construction, but I didn't want to turn on general query logging for the MySQL server.

The application uses the object-oriented style of the MySQLi PHP extension so this "bolt-on" wrapper for the MySQLi class works and all I need to do is,
paste the code before my "$db = new mysqli(...)" line,
prepend an underscore to that call,
add a "print_r($db->queries)" at the end of the script,
and I've got a "good enough" query logger without needing to taint my actual DB code.

PHP Code: [ Select ]
<?php
 
class _mysqli
{
   private $db;
   public $queries;
 
   public function __construct($host, $username, $password, $database, $port, $socket)
   {
      $this->db = new mysqli($host, $username, $password, $database, $port, $socket);
      $this->queries = array();
   }
   public function __call($func, $args)
   {
      return call_user_func_array(array($this->db, $func), $args);
   }
   public function __get($key)
   {
      return $this->db->$key;
   }
   public function query($sql, $mode = MYSQLI_STORE_RESULT)
   {
      $this->queries[] = $sql;
      return $this->db->query($sql, $mode);
   }
   public function prepare($sql)
   {
      $this->queries[] = $sql;
      return $this->db->prepare($sql);
   }
}
  1. <?php
  2.  
  3. class _mysqli
  4. {
  5.    private $db;
  6.    public $queries;
  7.  
  8.    public function __construct($host, $username, $password, $database, $port, $socket)
  9.    {
  10.       $this->db = new mysqli($host, $username, $password, $database, $port, $socket);
  11.       $this->queries = array();
  12.    }
  13.    public function __call($func, $args)
  14.    {
  15.       return call_user_func_array(array($this->db, $func), $args);
  16.    }
  17.    public function __get($key)
  18.    {
  19.       return $this->db->$key;
  20.    }
  21.    public function query($sql, $mode = MYSQLI_STORE_RESULT)
  22.    {
  23.       $this->queries[] = $sql;
  24.       return $this->db->query($sql, $mode);
  25.    }
  26.    public function prepare($sql)
  27.    {
  28.       $this->queries[] = $sql;
  29.       return $this->db->prepare($sql);
  30.    }
  31. }
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Yeah I love PHP 5, I sent you something in your PMs box. I should fix it up a bit though I suppose.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13504
  • Loc: Florida

Post 3+ Months Ago

Thanks for that.

I don't know why extending the mysqli class didn't dawn on me. I think I've got the mindset that the built-in and extension provided stuff isn't alterable in PHP, which I'm confusing with un-extendable. :scratchhead:

I'll probably boil this all down to an extension with with just the query/prepare methods next time I need it. :D
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

Yeah I guess I just did it and it worked. I never really thought it couldn't happen, I think I just saw some examples or read about it somewhere. That's what's cool when you get into more OOP stuff in PHP5, they let you mess with stuff they already have. I wish I could fix this stupid error handler though.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 89 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.