0

I need to get the complete query with the filled values after the query got successfully executed, (basically I want the query string with the question marks in the query removed and get replaced with corresponding values) so that I can use it for transaction logging.

below are the lines of code that I am using

$stmt1 = $dbh->prepare("INSERT INTO announcements(id,routeID,ServiceAdvisoryID,ServiceAdvisoryDetailsId) 
VALUES (?,?,?,?)");
        $stmt1->execute(array($aid,$route,$anctype,$announcementid));

the query used for transaction logging is

    $transactionText = "INSERT INTO announcements(id,routeID,ServiceAdvisoryID,ServiceAdvisoryDetailsId) VALUES (?,?,?,?)";
$stmt2 = $dbh->prepare("insert into transactionLog_tbl(userName,transactionTypeId,transactionTime,transactionText)values(?,?,?,?)");
            $stmt2->execute(array($_SESSION['username'],1,date("y.d.m"),$transactionText));

I want transaction text to have question marks filled with corresponding values.

or else

Can i get the last executed ROW by a PDO. I know we can get the ID but cam we get the complete row as well?

Any help would be greatly appreciated.

rookie_developer
  • 1,359
  • 3
  • 15
  • 27
  • 1
    Take a look at the answers to this question : http://stackoverflow.com/questions/2411182/how-to-debug-pdo-database-queries ; there are some informations there that might help. – Pascal MARTIN May 18 '12 at 19:53

1 Answers1

2

If you're using MySQL you can log there and they will (most likely) have the complete SQL statement. This is because PDO by default emulates prepares and actually sends fully built SQL strings to MySQL server. From the manual's PDO::setAttribute page:

PDO::ATTR_EMULATE_PREPARES Enables or disables emulation of prepared statements. Some drivers do not support native prepared statements or have limited support for them. Use this setting to force PDO to either always emulate prepared statements (if TRUE), or to try to use native prepared statements (if FALSE). It will always fall back to emulating the prepared statement if the driver cannot successfully prepare the current query.

The PHP dev team states that the reason for this is that emulation performs much faster on relatively older versions of MySQL that are still on the majority of LAMP stack installations. This default behavior is set to change soon as more people use newer versions of MySQL + native driver.

Otherwise, you can just construct the full SQL yourself since you already have both the statement and the parameters.

webbiedave
  • 48,414
  • 8
  • 88
  • 101
  • Thanks for the reply @webbiedave .. yeah I can construct it maually .. but the problem is that I have do so in HUGE number of pages .. Yes, I am using MySQL .. I am not sure how can we get the executed query with the values filled in question marks – rookie_developer May 18 '12 at 22:39