In PHP + PDO fetch the details of the query last executed or fill the question marks in query with corresponding entries

孤街醉人 提交于 2019-12-25 00:26:20

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/10658865/in-php-pdo-fetch-the-details-of-the-query-last-executed-or-fill-the-question-m

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!