Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a PDO query string with bound parameters without executing it

Tags:

sql

php

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; 

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?

like image 851
VirtuosiMedia Avatar asked Feb 10 '09 00:02

VirtuosiMedia


1 Answers

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); 
like image 112
2 revs Avatar answered Oct 14 '22 08:10

2 revs