├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── composer.json ├── pdo-debug.php ├── pdo-debug.sql ├── phpunit.xml.dist └── tests ├── PdoDebugger ├── PdoDebuggerIssuesTest.php └── PdoDebuggerTest.php └── bootstrap.php /.gitignore: -------------------------------------------------------------------------------- 1 | vendor/ 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | php: 3 | - 5.3 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | 8 | install: composer dump-autoload 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 panique 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pdo-debug 2 | 3 | Shows the SQL query constructed by PDO. Seriously. The magic behind: A simple function that combines your parameters 4 | and the raw query. Please note that this is just an emulation, not the "real" result of PDO, but it does the job for 5 | nearly all common daily tasks. Way better than NOTHING! 6 | 7 | ## Features 8 | 9 | - one simple global function `debugPDO($sql, $parameters)` 10 | - works with named parameters (like ':param_1') and question-mark-parameters (like '?') 11 | - this repo also contains a demo .sql file for easily testing the example below 12 | 13 | ## Big thanks to 14 | 15 | Taken from here: http://stackoverflow.com/questions/210564/getting-raw-sql-query-string-from-pdo-prepared-statements, 16 | big big big thanks to bigwebguy (http://stackoverflow.com/users/168256/bigwebguy) 17 | and Mike (http://stackoverflow.com/users/1083889/mike) for creating the debugQuery() function! 18 | 19 | ## How to add to a project 20 | 21 | As usual, require this via Composer (require-dev might be more useful as you definitely don't need this in production): 22 | 23 | ```json 24 | "require-dev": { 25 | "panique/pdo-debug": "0.2" 26 | } 27 | ``` 28 | 29 | ## How to use 30 | 31 | Your PDO block probably looks like this: 32 | ```php 33 | $sql = "INSERT INTO test (col1, col2, col3) VALUES (:col1, :col2, :col3)"; 34 | ``` 35 | and this, right ? 36 | ```php 37 | $query->execute(array(':col1' => $param_1, ':col2' => $param_2, ':col3' => $param_3)); 38 | ``` 39 | 40 | To use this PDO logger, you'll have to rebuild the param array a little bit: 41 | Create an array that has the identifier as the key and the parameter as the value, like below: 42 | **WARNING: write this WITHOUT the colon! The keys need to be 'xxx', not ':xxx'!** 43 | 44 | ```php 45 | $parameters = array( 46 | 'param1' => 'hello', 47 | 'param2' => 123, 48 | 'param3' => null 49 | ); 50 | ``` 51 | 52 | Your full PDO block would then look like: 53 | 54 | ```php 55 | $sql = "INSERT INTO test (col1, col2, col3) VALUES (:param1, :param2, :param3)"; 56 | $query = $database_connection->prepare($sql); 57 | $query->execute($parameters); 58 | ``` 59 | 60 | Now you can debug / log the full SQL statement by using the static method `show()` of the PdoDebugger class. Make sure 61 | to pass the raw SQL statement and the parameters array that contains proper keys and values. Future releases might have 62 | a more professional way of handling this. 63 | 64 | ```php 65 | echo PdoDebugger::show($sql, $parameters); 66 | ``` 67 | 68 | The result of this example will be: 69 | 70 | ```sql 71 | INSERT INTO test (col1, col2, col3) VALUES ('hello', 123, NULL) 72 | ``` 73 | 74 | Yeah! 75 | 76 | ## Support the project (and others) 77 | 78 | Support the project by renting a server at [DigitalOcean](https://www.digitalocean.com/?refcode=40d978532a20) or just tipping a coffee at BuyMeACoffee.com. Thanks! :) 79 | 80 | Buy Me A Coffee 81 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "panique/pdo-debug", 3 | "description": "A super-simple function that returns the full SQL query from your PDO statements", 4 | "keywords": ["pdo", "debug", "debugger", "log", "logger", "sql"], 5 | "homepage": "https://github.com/panique/pdo-debug", 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Panique", 10 | "role": "Developer" 11 | } 12 | ], 13 | "autoload": { 14 | "files": ["pdo-debug.php"] 15 | } 16 | } -------------------------------------------------------------------------------- /pdo-debug.php: -------------------------------------------------------------------------------- 1 | $value) { 35 | 36 | // check if named parameters (':param') or anonymous parameters ('?') are used 37 | if (is_string($key)) { 38 | $keys[] = '/:'.ltrim($key, ':').'/'; 39 | } else { 40 | $keys[] = '/[?]/'; 41 | } 42 | 43 | // bring parameter into human-readable format 44 | if (is_string($value)) { 45 | $values[] = "'" . addslashes($value) . "'"; 46 | } elseif(is_int($value)) { 47 | $values[] = strval($value); 48 | } elseif (is_float($value)) { 49 | $values[] = strval($value); 50 | } elseif (is_array($value)) { 51 | $values[] = implode(',', $value); 52 | } elseif (is_null($value)) { 53 | $values[] = 'NULL'; 54 | } 55 | } 56 | if ($isNamedMarkers) { 57 | return preg_replace($keys, $values, $raw_sql); 58 | } else { 59 | return preg_replace($keys, $values, $raw_sql, 1, $count); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /pdo-debug.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `test` ( 2 | `id` int(11) NOT NULL AUTO_INCREMENT, 3 | `col1` text COLLATE utf8_unicode_ci NOT NULL, 4 | `col2` int(11) NOT NULL, 5 | `col3` tinyint(1) DEFAULT NULL, 6 | PRIMARY KEY (`id`) 7 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ; -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | tests/PdoDebugger 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /tests/PdoDebugger/PdoDebuggerIssuesTest.php: -------------------------------------------------------------------------------- 1 | (SELECT lastrun FROM `config` WHERE username = :username) 17 | OR ut.lastmodifieddate > (SELECT lastrun FROM `config` WHERE username = :username) 18 | OR t.lastmodifieddate > (SELECT lastrun FROM `config` WHERE username = :username) 19 | eof; 20 | 21 | $expected = << (SELECT lastrun FROM `config` WHERE username = 'johndoe') 31 | OR ut.lastmodifieddate > (SELECT lastrun FROM `config` WHERE username = 'johndoe') 32 | OR t.lastmodifieddate > (SELECT lastrun FROM `config` WHERE username = 'johndoe') 33 | eof; 34 | 35 | $params = array( 36 | 'username' => 'johndoe', 37 | ); 38 | 39 | $res = PdoDebugger::show($sql, $params); 40 | $this->assertEquals($res, $expected); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /tests/PdoDebugger/PdoDebuggerTest.php: -------------------------------------------------------------------------------- 1 | assertEquals($res, 'INSERT INTO users(login, password, email) VALUES (\'jdoe\', \'p4$$w0rd\', \'john.doe@example.com\')'); 18 | } 19 | 20 | public function testMarkers() 21 | { 22 | $sql = 'INSERT INTO users(login, password, email) VALUES (:login, :password, :email)'; 23 | $params = array( 24 | 'login' => 'jdoe', 25 | 'password' => 'p4$$w0rd', 26 | 'email' => 'john.doe@example.com', 27 | ); 28 | $res = PdoDebugger::show($sql, $params); 29 | $this->assertEquals($res, 'INSERT INTO users(login, password, email) VALUES (\'jdoe\', \'p4$$w0rd\', \'john.doe@example.com\')'); 30 | } 31 | 32 | public function testEscapeQuotes() 33 | { 34 | $sql = 'INSERT INTO users(login, password, email) VALUES (:login, :password, :email)'; 35 | $params = array( 36 | 'login' => 'jdoe', 37 | 'password' => 'p4$\'$w0rd', 38 | 'email' => 'john.doe@example.com', 39 | ); 40 | $res = PdoDebugger::show($sql, $params); 41 | $this->assertEquals($res, 'INSERT INTO users(login, password, email) VALUES (\'jdoe\', \'p4$\\\'$w0rd\', \'john.doe@example.com\')'); 42 | } 43 | 44 | public function testColumnsInParams() 45 | { 46 | $sql = 'INSERT INTO users(login, password, email) VALUES (:login, :password, :email)'; 47 | $params = array( 48 | ':login' => 'jdoe', 49 | 'password' => 'p4$$w0rd', 50 | 'email' => 'john.doe@example.com', 51 | ); 52 | $res = PdoDebugger::show($sql, $params); 53 | $this->assertEquals($res, 'INSERT INTO users(login, password, email) VALUES (\'jdoe\', \'p4$$w0rd\', \'john.doe@example.com\')'); 54 | } 55 | 56 | public function testMultipleOccurrences() 57 | { 58 | $sql = 'SELECT * FROM users WHERE username LIKE :user OR username LIKE :username OR email LIKE :user'; 59 | $params = array( 60 | 'user' => '%jdoe%', 61 | 'username' => '%j.doe%', 62 | ); 63 | $res = PdoDebugger::show($sql, $params); 64 | $this->assertEquals($res, 'SELECT * FROM users WHERE username LIKE \'%jdoe%\' OR username LIKE \'%j.doe%\' OR email LIKE \'%jdoe%\''); 65 | } 66 | 67 | public function testNumbers() 68 | { 69 | $sql = 'SELECT * FROM test WHERE float_val > :float AND int_val > :int'; 70 | $params = array( 71 | 'float' => 3.14159, 72 | 'int' => 42 73 | ); 74 | $res = PdoDebugger::show($sql, $params); 75 | $this->assertEquals($res, 'SELECT * FROM test WHERE float_val > 3.14159 AND int_val > 42'); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /tests/bootstrap.php: -------------------------------------------------------------------------------- 1 |