問題描述
是否可以在不先執(zhí)行的情況下從帶有綁定參數(shù)的 PDO 對(duì)象中獲取查詢字符串?我有類似于以下的代碼(其中 $dbc 是 PDO 對(duì)象):
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 = ?".但是,我希望包含綁定參數(shù),使其看起來像:'SELECT * FROM users WHERE username = 'bob'".有沒有辦法在不執(zhí)行它或通過某些東西用參數(shù)替換問號(hào)的情況下做到這一點(diǎn)像 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?
推薦答案
簡而言之:沒有.請(qǐng)參閱從 PDO 準(zhǔn)備好的語句中獲取原始 SQL 查詢字符串
In short: no. See Getting raw SQL query string from PDO prepared statements
如果您只想模擬它,請(qǐng)嘗試:
If you want to just emulate it, try:
echo preg_replace('?', $username, $result->queryString);
這篇關(guān)于獲取帶有綁定參數(shù)的 PDO 查詢字符串而不執(zhí)行它的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!