Greg Sabino Mullane wrote:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160


I need to log every INSERT, UPDATE and DELETE queries even when using
placeholders. Here some code:

$sql = "UPDATE users SET `name`=? WHERE d`=1;";
$sth = $dbh->prepare($sql);
$sth->execute('Test User');
$sth->finish();

Of course execute params are given dynamically and I want to use
placeholders for more secure code.
I want to save that UPDATE query into file or database ( I'll prefer
DB :) ) for tracking purposes. Any idea how to do this?

The canonical way is to use the named tracing level 'SQL', like so:

$dbh->trace('SQL');

However, it's not supported on all DBDs yet (actually, DBD::Pg may be
the only one, but I'm not sure). From your non-standard use of backticks
above, I presume you are using DBD::mysql, which, from a quick glance
at the code, does not support 'SQL' tracing yet. Another option may be to
simply set a high numeric trace level: the queries will be logged, albeit
surrounded by a lot of noise (but parsing such things out is one of
the things Perl is good at, right?). You can also log such queries from
the database itself, of course (e.g. the db logs)

DBIx::Log4perl may work too, but it doesn't seem to mark the executes in
any way that tracks back to the statement that prepared them.


That is a good point and I will correct that soon.

Martin
--
Martin J. Evans
Easysoft Limited
http://www.easysoft.com

Reply via email to