問題描述
是否可以在不先執行的情況下從帶有綁定參數的 PDO 對象中獲取查詢字符串?我有類似于以下的代碼(其中 $dbc 是 PDO 對象):
Is it possible to get a query string from a PDO object with bound parameters without executing it first? I have code similar to the following (where $dbc is the PDO object):
$query = 'SELECT * FROM users WHERE username = ?';
$result = $dbc->prepare($query);
$username = 'bob';
$result->bindParam(1, $username);
echo $result->queryString;
目前,這將輸出一條 SQL 語句,例如:SELECT * FROM users WHERE username = ?".但是,我希望包含綁定參數,使其看起來像:'SELECT * FROM users WHERE username = 'bob'".有沒有辦法在不執行它或通過某些東西用參數替換問號的情況下做到這一點像 preg_replace?
Currently, this will echo out a SQL statement like: "SELECT * FROM users WHERE username = ?". However, I would like to have the bound parameter included so that it looks like: 'SELECT * FROM users WHERE username = 'bob'". Is there a way to do that without executing it or replacing the question marks with the parameters through something like preg_replace?
推薦答案
簡而言之:沒有.請參閱從 PDO 準備好的語句中獲取原始 SQL 查詢字符串
In short: no. See Getting raw SQL query string from PDO prepared statements
如果您只想模擬它,請嘗試:
If you want to just emulate it, try:
echo preg_replace('?', $username, $result->queryString);
這篇關于獲取帶有綁定參數的 PDO 查詢字符串而不執行它的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!