Getting raw SQL query string from PDO prepared statements

前端 未结 16 1055
心在旅途
心在旅途 2020-11-22 06:56

Is there a way to get the raw SQL string executed when calling PDOStatement::execute() on a prepared statement? For debugging purposes this would be extremely useful.

16条回答
  •  独厮守ぢ
    2020-11-22 07:06

    preg_replace didn't work for me and when binding_ was over 9, binding_1 and binding_10 was replaced with str_replace (leaving the 0 behind), so I made the replacements backwards:

    public function interpolateQuery($query, $params) {
    $keys = array();
        $length = count($params)-1;
        for ($i = $length; $i >=0; $i--) {
                $query  = str_replace(':binding_'.(string)$i, '\''.$params[$i]['val'].'\'', $query);
               }
            // $query  = str_replace('SQL_CALC_FOUND_ROWS', '', $query, $count);
            return $query;
    

    }

    Hope someone finds it useful.

提交回复
热议问题