如何查看准备好的PDO SQL语句[重复]

时间:2022-09-15 15:34:36

Possible Duplicate:
PDO Prepared Statements

可能重复:PDO准备的声明

I'm sure the answer to this is very simple, but I don't seem to be able to find it.

我确定答案很简单,但我似乎无法找到它。

I'm using PDO (PHP Data Objects) to run a query against a MySQL database, and would find it useful to display the prepared query before it is executed against the DB.

我正在使用PDO(PHP数据对象)对MySQL数据库运行查询,并且发现在对数据库执行之前显示准备好的查询很有用。

Is there any way of doing this? For example:

有没有办法做到这一点?例如:

$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();

3 个解决方案

#1


3  

You can't get the query which is sent to server because PDO doesn't work this way.

您无法获取发送到服务器的查询,因为PDO不能以这种方式工作。

It sends the $query seperately and $id seperately to the server-database which are executed after joining by database.

它将$ query和$ id分别发送到服务器数据库,这些数据库在加入数据库后执行。

#2


3  

A frequent practice is to print the query (which has placeholders in it) alongside the bound values. When using an array of the style :placeholder => value you can just var_dump, print_r or var_export the array.

通常的做法是打印查询(其中包含占位符)和绑定值。当使用样式数组:placeholder => value时,你可以只使用var_dump,print_r或var_export数组。

This is done in Magento SQL debugging for example.

例如,这在Magento SQL调试中完成。

The "final" query doesn't exist as a string unless the PDO driver doesn't support prepared statements and it's simulating them.

“final”查询不作为字符串存在,除非PDO驱动程序不支持预准备语句并且它正在模拟它们。

In essence you can think of prepared statements as if they were a stored function or a stored procedure. You create it once and execute it multiple times with multiple parameters.

实质上,您可以将预准备语句视为存储函数或存储过程。您创建一次并使用多个参数多次执行它。

#3


3  

use it:

用它:

/**
 * 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: View and debug prepared PDO query without looking at MySQL logs

source:查看和调试准备好的PDO查询,而不查看MySQL日志

#1


3  

You can't get the query which is sent to server because PDO doesn't work this way.

您无法获取发送到服务器的查询,因为PDO不能以这种方式工作。

It sends the $query seperately and $id seperately to the server-database which are executed after joining by database.

它将$ query和$ id分别发送到服务器数据库,这些数据库在加入数据库后执行。

#2


3  

A frequent practice is to print the query (which has placeholders in it) alongside the bound values. When using an array of the style :placeholder => value you can just var_dump, print_r or var_export the array.

通常的做法是打印查询(其中包含占位符)和绑定值。当使用样式数组:placeholder => value时,你可以只使用var_dump,print_r或var_export数组。

This is done in Magento SQL debugging for example.

例如,这在Magento SQL调试中完成。

The "final" query doesn't exist as a string unless the PDO driver doesn't support prepared statements and it's simulating them.

“final”查询不作为字符串存在,除非PDO驱动程序不支持预准备语句并且它正在模拟它们。

In essence you can think of prepared statements as if they were a stored function or a stored procedure. You create it once and execute it multiple times with multiple parameters.

实质上,您可以将预准备语句视为存储函数或存储过程。您创建一次并使用多个参数多次执行它。

#3


3  

use it:

用它:

/**
 * 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: View and debug prepared PDO query without looking at MySQL logs

source:查看和调试准备好的PDO查询,而不查看MySQL日志