Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Last Executed Query in PHP PDO

Tags:

php

pdo

I would like to know what query is executed using PHP PDO. I have:

<?php

try {  
  $DBH = new PDO("mysql:host=localhost;dbname=mytable", 'myuser', 'mypass');  
}  
catch(PDOException $e) {  
    echo $e->getMessage();  
}  

$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );  
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );  

$STH = $DBH->("INSERT INTO mytable (column1, column2, column3 /* etc...*/) value (:column1, :column2, :column3 /* etc...*/)"); 
$STH->bindParam(':column1', $column1);  
$STH->bindParam(':column2', $column2);  
$STH->bindParam(':column3', $column3);  
 /* etc...*/

$STH->execute();  

// what is my query?

I would like to get something like:

INSERT INTO mytable (column1, column2, column3) value ('my first column', 32, 'some text')

Is it possible? Thanks

like image 801
Tech4Wilco Avatar asked Oct 10 '11 18:10

Tech4Wilco


People also ask

How can I check my PDO statement?

The easy and best way is to use SET global general_log = 1; and SET global log_output = 'table'; then simply query from the mysql database as SELECT * FROM mysql. general_log . It shows the prepare statement and and the actual query executed on the database.

What does PDO -> query return?

PDO::query() returns a PDOStatement object, or FALSE on failure.

What is PDO query in PHP?

PDO::query() prepares and executes an SQL statement in a single function call, returning the statement as a PDOStatement object.

What is a query in PHP?

Definition and Usage. The query() / mysqli_query() function performs a query against a database.


2 Answers

<?php  class MyPDOStatement extends PDOStatement {   protected $_debugValues = null;    protected function __construct()   {     // need this empty construct()!   }    public function execute($values=array())   {     $this->_debugValues = $values;     try {       $t = parent::execute($values);       // maybe do some logging here?     } catch (PDOException $e) {       // maybe do some logging here?       throw $e;     }      return $t;   }    public function _debugQuery($replaced=true)   {     $q = $this->queryString;      if (!$replaced) {       return $q;     }      return preg_replace_callback('/:([0-9a-z_]+)/i', array($this, '_debugReplace'), $q);   }    protected function _debugReplace($m)   {     $v = $this->_debugValues[$m[1]];     if ($v === null) {       return "NULL";     }     if (!is_numeric($v)) {       $v = str_replace("'", "''", $v);     }      return "'". $v ."'";   } }  // have a look at http://www.php.net/manual/en/pdo.constants.php $options = array(   PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,   PDO::ATTR_STATEMENT_CLASS => array('MyPDOStatement', array()), );  // create PDO with custom PDOStatement class $pdo = new PDO($dsn, $username, $password, $options);  // prepare a query $query = $pdo->prepare("INSERT INTO mytable (column1, column2, column3)   VALUES (:col1, :col2, :col3)");  // execute the prepared statement $query->execute(array(   'col1' => "hello world",   'col2' => 47.11,   'col3' => null, ));  // output the query and the query with values inserted var_dump( $query->queryString, $query->_debugQuery() ); 
like image 175
rodneyrehm Avatar answered Sep 29 '22 05:09

rodneyrehm


Most people create a wrapper class around the PDO object to record the queries as they are sent to the database. Hardly anyone uses a direct PDO object since you can add extra helper methods by wrapping, or extending PDO.

/**
 * Run a SQL query and return the statement object
 *
 * @param string $sql query to run
 * @param array $params the prepared query params
 * @return PDOStatement
 */
public function query($sql, array $params = NULL)
{
    $statement = $this->pdo->prepare($sql);

    $statement->execute($params);

    // Save query results by database type
    self::$queries[] = $sql;

    return $statement;
}
like image 37
Xeoncross Avatar answered Sep 29 '22 05:09

Xeoncross