【问题标题】:View and debug prepared PDO query without looking at MySQL logs无需查看 MySQL 日志即可查看和调试准备好的 PDO 查询
【发布时间】:2023-03-13 06:35:01
【问题描述】:

我想在不查看 MySQL 日志的情况下查看 PDO 正在准备什么。基本上是它在执行查询之前构建的最终查询。

有没有办法做到这一点?

【问题讨论】:

    标签: php mysql pdo


    【解决方案1】:

    没有内置的方法可以做到这一点。 bigwebguy 创建了一个函数来做到这一点in one of his answers:

    /**
     * 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;
    }
    

    【讨论】:

      【解决方案2】:

      这只是上面@Maerlyn 代码的派生,它接受参数的非关联数组,如果键已经以':'开头,请不要添加它。

      /**
       * 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
       * 
       * @author maerlyn https://stackoverflow.com/users/308825/maerlyn
       */
      function interpolateQuery($query, $params) {
          $keys = array();
      
          # build a regular expression for each parameter
      
          if (!isAssoc($params)){
              $_params = [];  // associative array
              foreach($params as $param){
                  $key = $param[0];
                  $value = $param[1];
                  // $type = $param[2];
                  $_params[$key] = $value;
              }
              $params  = $_params;
          }       
      
          foreach ($params as $key => $value) {
              if (is_string($key)) {
                  $keys[] = '/'.((substr($key,0,1)==':') ? '' : ':').$key.'/';
              } else {
                  $keys[] = '/[?]/';
              }
          }
      
          $query = preg_replace($keys, $params, $query, 1, $count);
      
          #trigger_error('replaced '.$count.' keys');
      
          return $query;
      }
      

      【讨论】:

        猜你喜欢
        • 2013-11-19
        • 2012-06-22
        • 2012-06-24
        • 2011-11-11
        • 1970-01-01
        • 1970-01-01
        • 2012-05-16
        • 1970-01-01
        相关资源
        最近更新 更多