maximebf / php-debugbar

Debug bar for PHP
phpdebugbar.com
MIT License
4.18k stars 399 forks source link

integration with mysqli #326

Open Geethikamanat opened 7 years ago

Geethikamanat commented 7 years ago

i am using mysqli not pdo, how to integrate this with mysqli? i am saving queries in function function dbquery($sql) {

$msc = microtime(true);
$result = mysqli_query($GLOBALS["link"], $sql);
$msc = microtime(true)-$msc; 
$duration =  ($msc )  ;  //seconds 
$query[] = $sql.'----'.$duration;  

return $result; }

and added a mysql collector call collector using

$collector = new mysqlCollector($query); 
$debugbar->addCollector($collector);

but it doesnt includes ajax queries. for ajax requests i set $debugbar->sendDataInHeaders(true); but it doesnt works collector

namespace DebugBar\Bridge; use DebugBar\DataCollector\AssetProvider; use DebugBar\DataCollector\DataCollector; use DebugBar\DataCollector\Renderable; use DebugBar\DebugBarException;

class mysqlCollector extends DataCollector implements Renderable, AssetProvider { protected $mysql_queries;

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

public function collect()
{  
    $queries = array();
    $totalExecTime = 0;
    foreach ($this->mysql_queries as $q) {  

        $query = explode('----',$q); 
        $queries[] = array(
            'sql' => $query[0],
            'duration' => $query[1],
            'duration_str' => $this->formatDuration($query[1])
        );
        $totalExecTime += $query[1];
    } 
    return array(
        'nb_statements' => count($queries),
        'accumulated_duration' => $totalExecTime,
        'accumulated_duration_str' => $this->formatDuration($totalExecTime),
        'statements' => $queries
    );
}

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

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

public function getAssets()
{
    return array(
        'css' => 'widgets/sqlqueries/widget.css',
        'js' => 'widgets/sqlqueries/widget.js'
    );
}
public function query($statement)
{
    return $this->profileCall('query', $statement, func_get_args());
}
protected function profileCall($method, $sql, array $args)
{

        $result = call_user_func_array(array($this->mysql_queries, $method), $args);print_r($result);

    return $result;
}
  public function addExecutedStatement( $stmt)
{
    $this->mysql_queries = $stmt; 
}

}

devnix commented 5 years ago

Are you sure you are calling $debugbar->sendDataInHeaders(true); at the end of the script, just before echoing any output? I've fall in that little pit a few moments ago, I feel the name or the behavour of that function confusing (and not documented).

garbinmarcelo commented 1 year ago

Hello, I have a legacy project (PHP 5) that uses Kohana v2 and would like to know how can I log the SQL? Could help? Thanks

ihlassovbetov commented 5 months ago

i also have custom php framework which is made by procedural php mainly. I added PHP DebugBar, except database it works. For Database tab, I did custom mysqli collector, although it does not procude error, it does not work. Can you make an official example for mysqli integration @maximebf ? I have tried to catch queries with "$this->mysqli->query("SHOW FULL PROCESSLIST");"

parallels999 commented 5 months ago

I did custom mysqli collector, although it does not procude error, it does not work

@ihlassovbetov hi, share your progress and maybe we can review why it is not working for you, an official PR could be made

Also, ideas https://github.com/maximebf/php-debugbar/issues/517#issuecomment-1724366970, https://github.com/maximebf/php-debugbar/issues/213, Collectors/DatabaseMySqliQueryCollector.php#L58 DataCollector/MysqlCollector.php#L37