php-debugbar icon indicating copy to clipboard operation
php-debugbar copied to clipboard

How to use with MySQL

Open Mte90 opened this issue 11 years ago • 3 comments

I'm trying to integrare in wordpress but i don't understand how to port this in Mysql.

    $pdoRead = new DebugBar\DataCollector\PDO\TraceablePDO( new PDO( 'sqlite::memory:' ) );
    $pdoWrite = new DebugBar\DataCollector\PDO\TraceablePDO( new PDO( 'sqlite::memory:' ) );

Mte90 avatar Apr 28 '15 10:04 Mte90

Wordpress uses mysqli, not PDO right? According to the codex, you can enable query logging.

define( 'SAVEQUERIES', true );
$queries = $wpdb->queries;

Take the DoctrineCollector for example, but use the $wpdb variable instead.

Roughly something like this:

$collector = new WordpressDatabaseCollector($wpdb);
$debugbar->addCollector($collector);
<?php
use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
use DebugBar\DebugBarException;

class WordpressDatabaseCollector extends DataCollector implements Renderable, AssetProvider
{
    protected $wpdb;

    public function __construct($wpdb)
    {
        $this->wpdb = $wpdb;
    }

    public function collect()
    {
        $queries = array();
        $totalExecTime = 0;
        foreach ($this->wpdb->queries as $q) {
            list($query, $duration, $caller) = $q;
            $queries[] = array(
                'sql' => $query,
                'duration' => $duration,
                'duration_str' => $this->formatDuration($duration)
            );
            $totalExecTime += $duration;
        }

        return array(
            'nb_statements' => count($queries),
            'accumulated_duration' => $totalExecTime,
            'accumulated_duration_str' => $this->formatDuration($totalExecTime),
            'statements' => $queries
        );
    }

    public function getName()
    {
        return 'wpdb';
    }

    public function getWidgets()
    {
        return array(
            "database" => array(
                "icon" => "arrow-right",
                "widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
                "map" => "wpdb",
                "default" => "[]"
            ),
            "database:badge" => array(
                "map" => "wpdb.nb_statements",
                "default" => 0
            )
        );
    }

    public function getAssets()
    {
        return array(
            'css' => 'widgets/sqlqueries/widget.css',
            'js' => 'widgets/sqlqueries/widget.js'
        );
    }
}

barryvdh avatar Apr 28 '15 11:04 barryvdh

Works perfect. Thx a lot!

Svobi avatar Aug 12 '15 15:08 Svobi

Hello, would you know if I could do this to get the SQL queries in a legacy project? or even in pure mysql queries?

Currently Kohana v2 is used and in some controllers it is done: Database::instance()->query($query); to get insert, update, select and delete. The project does not use queries via Model.

I thank the attention

garbinmarcelo avatar Jan 10 '23 12:01 garbinmarcelo