How to view a prepared PDO SQL query

Possible duplicate:
Prepared PDO reports

I am sure that the answer to this question is very simple, but I seem to be unable to find it.

I use PDO (PHP data objects) to run a query on a MySQL database and would find it useful to display the prepared query before executing it from the database.

Is there any way to do this? For instance:

$query = 'SELECT Id, Name, Comment FROM Users WHERE Id = :id';
$pdoStatement = $db->prepare($query);
$pdoStatement->bindValue(':id', $id);

// How can I view the actual statement that will be executed, showing the real
// value that will be used in place of ':id'

$pdoStatement->execute();
+5
source share
3 answers

You cannot receive a request that is sent to the server because PDO does not work this way.

It sends $ query separately and $ id separately to the server database, which are executed after joining the database.

+3
source

, ( ) . :placeholder => value var_dump, print_r var_export .

, , Magento SQL.

"" , PDO .

, , . .

+3

:

/**
 * Replaces any parameter placeholders in a query with the value of that
 * parameter. Useful for debugging. Assumes anonymous parameters from 
 * $params are are in the same order as specified in $query
 *
 * @param string $query The sql query with parameter placeholders
 * @param array $params The array of substitution parameters
 * @return string The interpolated query
 */
public static function interpolateQuery($query, $params) {
    $keys = array();

    # build a regular expression for each parameter
    foreach ($params as $key => $value) {
        if (is_string($key)) {
            $keys[] = '/:'.$key.'/';
        } else {
            $keys[] = '/[?]/';
        }
    }

    $query = preg_replace($keys, $params, $query, 1, $count);

    #trigger_error('replaced '.$count.' keys');

    return $query;
}

source: PDO- MySQL

+3
source

All Articles