2011-08-30 87 views
2

有没有办法让未经使用的东西一样print_r的与活动记录创建一个查询的结果()?笨:显示RAW查询结果

我了解Profiler in Codeigniter,但我需要的东西,也显示查询的输出,不仅查询本身(如探查器一样)。

谢谢!

+0

输出示例? – jondavidjohn

+0

喜欢的东西:SELECT * FROM汽车将输出: + --------- + ------------------ + | id_car |车牌| + --------- + ------------------ + | 123 | SBX-08-YXG | + --------- + ------------------ + –

+1

当你说RAW结果时,“Something like ...”并不是特定的, ,你必须有一些标准或例子的RAW结果,你试图效仿。当你说RAW时,你的意思是'mysql'命令行工具的输出吗? – jondavidjohn

回答

4

我开始研究这个问题,最后扩展了profiler类以打印查询结果。就像jondavidjohn一样,我对你真正想要的东西有点困惑,但是希望这会接近。你将要做的是创建一个应用程序/库称为MY_Profiler.php文件,然后将以下代码粘贴到其中:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class MY_Profiler extends CI_Profiler 
{ 
    protected function _compile_queries() 
    { 
     $dbs = array(); 

     // Let's determine which databases are currently connected to 
     foreach (get_object_vars($this->CI) as $CI_object) 
     { 
      if (is_object($CI_object) && is_subclass_of(get_class($CI_object), 'CI_DB')) 
      { 
       $dbs[] = $CI_object; 
      } 
     } 

     if (count($dbs) == 0) 
     { 
      $output = "\n\n"; 
      $output .= '<fieldset id="ci_profiler_queries" style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'; 
      $output .= "\n"; 
      $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').'&nbsp;&nbsp;</legend>'; 
      $output .= "\n"; 
      $output .= "\n\n<table style='border:none; width:100%;'>\n"; 
      $output .="<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px'>".$this->CI->lang->line('profiler_no_db')."</td></tr>\n"; 
      $output .= "</table>\n"; 
      $output .= "</fieldset>"; 

      return $output; 
     } 

     // Load the text helper so we can highlight the SQL 
     $this->CI->load->helper('text'); 

     // Key words we want bolded 
     $highlight = array('SELECT', 'DISTINCT', 'FROM', 'WHERE', 'AND', 'LEFT&nbsp;JOIN', 'ORDER&nbsp;BY', 'GROUP&nbsp;BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR&nbsp;', 'HAVING', 'OFFSET', 'NOT&nbsp;IN', 'IN', 'LIKE', 'NOT&nbsp;LIKE', 'COUNT', 'MAX', 'MIN', 'ON', 'AS', 'AVG', 'SUM', '(', ')'); 

     $output = "\n\n"; 

     $count = 0; 

     foreach ($dbs as $db) 
     { 
      $count++; 

      $hide_queries = (count($db->queries) > $this->_query_toggle_count) ? ' display:none' : ''; 

      $show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_hide').'\'?\''.$this->CI->lang->line('profiler_section_show').'\':\''.$this->CI->lang->line('profiler_section_hide').'\';">'.$this->CI->lang->line('profiler_section_hide').'</span>)'; 

      if ($hide_queries != '') 
      { 
       $show_hide_js = '(<span style="cursor: pointer;" onclick="var s=document.getElementById(\'ci_profiler_queries_db_'.$count.'\').style;s.display=s.display==\'none\'?\'\':\'none\';this.innerHTML=this.innerHTML==\''.$this->CI->lang->line('profiler_section_show').'\'?\''.$this->CI->lang->line('profiler_section_hide').'\':\''.$this->CI->lang->line('profiler_section_show').'\';">'.$this->CI->lang->line('profiler_section_show').'</span>)'; 
      } 

      $output .= '<fieldset style="border:1px solid #0000FF;padding:6px 10px 10px 10px;margin:20px 0 20px 0;background-color:#eee">'; 
      $output .= "\n"; 
      $output .= '<legend style="color:#0000FF;">&nbsp;&nbsp;'.$this->CI->lang->line('profiler_database').':&nbsp; '.$db->database.'&nbsp;&nbsp;&nbsp;'.$this->CI->lang->line('profiler_queries').': '.count($db->queries).'&nbsp;&nbsp;'.$show_hide_js.'</legend>'; 
      $output .= "\n"; 
      $output .= "\n\n<table style='width:100%;{$hide_queries}' id='ci_profiler_queries_db_{$count}'>\n"; 

      if (count($db->queries) == 0) 
      { 
       $output .= "<tr><td style='width:100%;color:#0000FF;font-weight:normal;background-color:#eee;padding:5px;'>".$this->CI->lang->line('profiler_no_queries')."</td></tr>\n"; 
      } 
      else 
      { 
       foreach ($db->queries as $key => $val) 
       { 
        $time = number_format($db->query_times[$key], 4); 

        $query = $val; 

        $val = highlight_code($val, ENT_QUOTES); 

        foreach ($highlight as $bold) 
        { 
         $val = str_replace($bold, '<strong>'.$bold.'</strong>', $val); 
        } 

        $output .= "<tr><td style='padding:5px; vertical-align: top;width:1%;color:#900;font-weight:normal;background-color:#ddd;'>".$time."&nbsp;&nbsp;</td><td style='padding:5px; color:#000;font-weight:normal;background-color:#ddd;vertical-align:top;'>".$val."</td><td style='padding:5px; color:#000;font-weight:normal;background-color:#ddd;'><pre>".print_r($db->query($query)->result_array(), true)."</pre></td></tr>\n"; 
       } 
      } 

      $output .= "</table>\n"; 
      $output .= "</fieldset>"; 

     } 

     return $output; 
    } 
} 

这是非常相似的原有功能剖析类,我添加了这个(+变量名的变更):

<td style='padding:5px; color:#000;font-weight:normal;background-color:#ddd;'><pre>".print_r($db->query($query)->result_array(), true)."</pre></td> 

通过更改此代码,可以风格如何,结果出现。当然,由于这只是分析器的扩展,你需要确保分析器启用:

$this->output->enable_profiler(TRUE); 

探查然后将打印新的一列,结果在里面,它看起来像这样: profilerextension

希望这有助于!

+0

谢谢,正是我在找的东西。 –