問題描述
我在兩年前偶然發現了這個問題.
I stumbled upon this question from two years ago.
有沒有辦法在準備好的語句上調用 PDOStatement::execute() 時執行原始 SQL 字符串?出于調試目的,這將非常有用.
Is there a way to get the raw SQL string executed when calling PDOStatement::execute() on a prepared statement? For debugging purposes this would be extremely useful.
獲勝答案指出
[...] 你也可以得到你想要的設置 PDO 屬性PDO::ATTR_EMULATE_PREPARES.在這模式,PDO 將參數插入到SQL查詢并發送整個執行時查詢.
[...] You can also get what you want if you set the PDO attribute PDO::ATTR_EMULATE_PREPARES. In this mode, PDO interpolate parameters into the SQL query and sends the whole query when you execute().
但它沒有提到如何獲取結果查詢字符串.我知道在性能方面這是一個壞主意,但這在調試模式下不會打擾我.有人知道怎么做嗎?
But it doesn't mention how to get the resulting query string. I know it's a bad idea performance wise but that doesn't bother me in debug mode. Does anybody know how to do this?
PS 如果有什么辦法可以讓我重新打開/引起人們對原來兩年前的話題的關注而不是打開一個新話題,請告訴我.
推薦答案
我相信這是在這個問題中引用的原始問題中提到的.然而實際上應該有一種方法來檢索這些數據.
I believe this is mentioned in the original question that was reference in this one. However there is actually supposed to be a method for retrieving this data.
PDOStatement::debugDumpParams
然而,它目前并沒有按照文檔工作.這里有一個錯誤報告和補丁提交給它http://bugs.php.net/bug.php?id=52384 以防有人對它感興趣.在修復之前,您似乎只能使用查詢日志記錄或使用 PDO::ATTR_STATEMENT_CLASS 屬性設置自定義語句類.
However it isn't currently working as documented. There is a bug report and patch submitted for it here http://bugs.php.net/bug.php?id=52384 in case anyone is interested in voting on it. Until it's fixed it seems like you are left to use query logging or setting a custom statement class using the PDO::ATTR_STATEMENT_CLASS attribute.
這篇關于從 PDO 準備好的語句中檢索(或模擬)完整查詢的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!