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
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);
}
}
- <?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);
- }
- }
Strong with this one, the sudo is.