1

I'm using PDO with the sqlsrv drivers. I would like to print out all queries passed through the connection that's initalised in __construct.

My setup is as followed;

public function __construct() {
    try{
        $this->connRead     = new PDO( "sqlsrv:server=" . DB_SERVER . "; Database =" . DB_NAME, DB_USER_READ, DB_PASSWORD_READ);
        $this->connRead->setAttribute(  PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }catch(PDOException $e){ echo 'Connection failed: ' . $e->getMessage(); }
}
public function __destruct() { $this->connRead = null; $this->connWrite= null; }

I would then make a request/call as such;

$sSQL = "SELECT foo FROM Table"
$st = $this->connRead->prepare( $sSQL );
$st->execute();

On loading a page I would like to be able to print all queries that use $this->connRead in plain text to the browser. Is it possible to do this?

cn007b
  • 16,596
  • 7
  • 59
  • 74
atoms
  • 2,993
  • 2
  • 22
  • 43
  • Possible duplicate: http://stackoverflow.com/questions/5299669/how-to-see-query-history-in-sql-server-management-studio – feeela Jan 25 '16 at 10:04

2 Answers2

0

You can use: debugDumpParams, and that's all what PDO can provide for you.
You will receive statement and variables for this statement.
Moreover you can receive this info only for particular statement not for connection.
And there is no way to receive clear SQL, you can just receive something like:

SELECT :date AS `date`

And in case when your statement text will be too long - it will be truncated, see.

But all modern frameworks live with it...

Community
  • 1
  • 1
cn007b
  • 16,596
  • 7
  • 59
  • 74
  • thanks for the reference to `debugDumpParams` will come in useful. Shame there is nothing that can it there are collect the queries sent :( – atoms Jan 25 '16 at 15:50
0

The soloution I found was to create a Class (DataHandler) to handle any DB calls. It gets initalised with a connection string from within the calling class or page.

Queries can then be peformed as such; $oDH->RunCommand($sSQL, [params])

Because all the queries are sent through the DataHandler, I can log whatever I choose. i.e. execution time, all queries ran on page, bound params etc.

atoms
  • 2,993
  • 2
  • 22
  • 43