PHP code example of carlosekt / psql_debug

1. Go to this page and download the library: Download carlosekt/psql_debug library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

carlosekt / psql_debug example snippets


     define('SYSTEM_START_TIME', microtime(true));

    ...
    'sql_debug' => true //false
    ...

    if ($di->get('config')->sql_debug) {
        \CarlosEkt\PSQL_Debug::getInstance()->end(microtime(true));
    }

    //Example register a 'db' service in the container
    $di->set('db', function () use ($config) {
        $connection = new \Phalcon\Db\Adapter\Pdo\Mysql([
            'host' => $config->database->host,
            'username' => $config->database->username,
            'password' => $config->database->password,
            'dbname' => $config->database->dbname,
            'charset' => $config->database->charset
        ]);

        if ($config->sql_debug) {
            $eventsManager = new \Phalcon\Events\Manager();

            \CarlosEkt\PSQL_Debug::getInstance()->init(SYSTEM_START_TIME);
            $eventsManager->attach('db', function ($event) {
                /** @var Phalcon\Events\Event $event */
                if ($event->getType() === 'beforeQuery') {
                    \CarlosEkt\PSQL_Debug::getInstance()->queryStart(microtime(true));
                }
                if ($event->getType() === 'afterQuery') {
                    $sql = \CarlosEkt\PSQL_Debug::getInstance()->getLastQuery(true);
                    \CarlosEkt\PSQL_Debug::getInstance()->queryEnd($sql, microtime(true));
                }
            });
            $connection->setEventsManager($eventsManager);
        }
        return $connection;
    }, true);