Logo
  • PHP
    • HipHop / HHVM
    • Modern PHP
    • PHPStorm
    • LAMP
    • Laravel
    • Composer
    • PDO
  • JavaScript
    • node.js
    • AngularJS
  • CSS
    • SASS
    • “CSS4” (CSS level 4)
  • HTML
  • Git
  • LAMP
  • Vagrant
  • UI / UX
  • Architecture of …
  • Off-Topic
With ♥ from Berlin
July 8, 2014
Chris
Composer, Local Development, PDO, PHP, Refactoring
Comments Off on Debug PDO with this one-line function. Yeah!

Debug PDO with this one-line function. Yeah!

PreviousNext
pdo-debug

Update: This article is a little bit older, the tool has changed gently. Everthing this article still works exactly like described. But, to get the latest version (different syntax) and up-to-date install instructions please see the GitHub page.

You know the problem: Debugging PDO is a pain in the ass, as it’s “not really possible”. With the classic mysql_* and mysqli_* functions / methods you can easily create your SQL query and output it directly in PHP with echo, var_dump or – the correct way – see the content directly in your IDE’s debugger. A little reminder here: Learn real debugging, it will save your life! However, when using PDO you’ll get the advantages of automatic SQL injection protection, beautiful syntax and performance increases, but have to deal with the fact you’ll never really see the full SQL statement. This is a common problem and has been asked over and over again on the web. I was also frustrated with that, and therefore grabbed the best PDO query emulator function I could find, put it into a Composer project and released it on GitHub / packagist: pdo-debug.

Why does this problem exist ?

PDO sends the raw query (without real values) and the parameters independently to the database server. That’s it. So PHP will never know the full statement, as the final query is constructed by MySQL, not by PHP.

In theory, the possibilities to debug PDO are

1. Let MySQL log the finally created values. This needs a lot of configuration and is definitly the best way to go, as this shows the real, final statements.

2. Take the raw SQL with the placeholders, take the parameter values and simply combine them in the way PDO would do. This is not perfect, as PDO does / allows more than just replacing the placeholders with real values (like changing types etc.). It’s just an emulation, and a very basic one.

Prepare a PDO block for debuggin with pdo-debug

Your PDO block probably looks like this:

$sql = "INSERT INTO test (col1, col2, col3) VALUES (:col1, :col2, :col3)";

and this, right?

$query->execute(array(':col1' => $param_1, ':col2' => $param_2, ':col3' => $param_3));

To use this PDO logger, you’ll have to rebuild the param array a little bit: Create an array that has the identifier as the key and the parameter as the value, like below:
WARNING: write this WITHOUT the colon! The keys need to be ‘xxx’, not ‘:xxx’!

$parameters = array(
    'param1' => 'hello',
    'param2' => 123,
    'param3' => null
);

Your full PDO block would then look like:

$sql = "INSERT INTO test (col1, col2, col3) VALUES (:param1, :param2, :param3)";
$query = $database_connection->prepare($sql);
$query->execute($parameters);

Perfect!

Load pdo-debug into your project (with Composer) and use it

As Composer is the de facto standard, we’ll load this debug tools with Composer (note the require-dev as you don’t need a PDO debugger in production, just in development)

"require-dev": {
    "panique/pdo-debug": "0.1"
}

and perform an

composer update

to load this into your project. You can now use pdo-debug everywhere by using the global function debugPDO(). A simple real world example:

echo debugPDO($sql, $parameters);

This would echo out the full SQL statement including the parameter values, like

INSERT INTO test (col1, col2, col3) VALUES ('hello', 123, NULL)

Voila!

Big thanks

A big thanks to bigwebguy and Mike on this StackOverflow thread: Getting raw SQL query string from PDO prepared statements. These guys have created and improved the basic function that is used in this free and open-source project. Send them a Thanks-notice if pdo-debug saves you a lot of time and nerves!

Warning: This is highly experimental!

This little tool is super-basic. The underlaying function has a lot of upvotes on StackOverflow and already helped lots of people. However, it’s just a basic emulation, not looking at possible glitches when doing advanced PDO stuff. If you are advanced or an expert with PDO, it would be nice if you could add your part to the repo right on GitHub. Maybe it’s possible to make this function better by respecting PDO’s parameter options etc. – anyway, if you want to contribute, please do!

debugpdoPHPsql
Share this
twig

A 6min video introduction into Twig, the PHP templating engine

Cool video, excellently explained. Just 6 minutes, but features all the basics. Note that you can integrate Twig into your

Joshua Davis – my hero of Flash – in two excellent interviews (audio, video)

Joshua Davis is the reason why I’ve started to code. Seriously. Back in the late 90ies, the internet was a

node.js

PayPal drops Java, goes node.js / JavaScript

First: this is not a Java-vs.-AnyOtherLanguage diss, just an article about a very interesting development: Using “frontend-languages” for serious server-side

phpstorm 7.0 php

How to debug code on a remote server (or in vagrant box) with PHPStorm

Please also note: There are several methods to do remote debugging. This is the one that works without any browser

laracon-2014-eu-amsterdam

Laracon 2013 – Jordi Boggiano: In Depth Composer (47min video)

More videos of 2013’s and 2014’s Laracon events from US and EU on their Youtube channel.

Quick fix for 404 error in WordPress category / tag page

Just a quick fix for a common problem: Sometimes, especially after switching themes, wordpress will generate 404 errors on the

composer

How to install Composer on Windows 7 / 8 or Ubuntu

Installation on Windows 7 / 8 Installing Composer on Windows 7 or 8 is extremely easy: Get the latest version

angular js

Learn AngularJS in 20 (or 90) minutes with Dan Wahlin

Definitly two of the best and most up-to-date short-introductions into AngularJS: Dan Wahlin shows how to build a full little

Frontend Ops Conf 2014 – Paul Irish: Delivering The Goods In Under 1000ms (40min video)

https://www.youtube.com/watch?v=E5lZ12Z889k

ilovepreloaders – A tumblr collection of preloader animations

An excellent collection of preloaders (GIFs, Movies and real ready-to-copy&paste code) here in this wonderful blog: http://ilovepreloaders.tumblr.com/ This thing just

1/4

Categories

Search

phpstorm-8
PHPStorm 8 (early access version) released – for free
phpstorm 7.0 php
PHPStorm 7 has been released!
php uk conference
PHP Opcache Explained by Julien Pauli (video from PHP UK Conference 2014)
sass
New features in SASS 3.3 (a talk by SASS-creator Chris Eppstein)
php
How to use the PHP 5.5 password hashing functions
php
How to install PHP curl extension (in 5 seconds)
offf-2014
Europeans: Get ready for OFFF conference / festival in Barcelona, May 2014
phpstorm-github-code-color-syntax-theme
Get Github’s code colors in PHPStorm (2014 style)
phpstorm-8
Killer-feature in PHPStorm: Search everywhere
zend framework 3
First view on Zend Framework 3 by Matthew O’Phinney
Perfect HTML email templates for perfect HTML emails (outlook!) with INK
laracon-2014-eu-amsterdam
Laracon 2013 – Kapil Verma: Engineering Complex Applications with Laravel 4 (40min video)
php
Postmodern PHP: appserver.io, a multithreaded application server for PHP, written in PHP
PHPMyAdmin not found after installation ? Here’s a fix (Ubuntu 12.04) !
mod-rewrite-ubuntu-14-04-lts
EOL lists of Ubuntu, Debian and CentOS for your server plannings

Tags

apache bash centos composer conference coupon CSS debian fonts framework git GitHub hack HHVM HipHop HTML HTML5 IDE JavaScript JS LAMP laravel linux mod_rewrite MVC MySQL Nginx optimization PHP PHP 5.5 PHP 5.6 phpmyadmin PHPStorm security server SSD Ubuntu UI UX vagrant video virtual machine voucher VPS wordpress
Side-Project: Wordle-Solver:
www.wordle-helper.info

Pages

  • Privacy Policy