2011-08-30 47 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

謝謝,正是我在找的東西。 –