Codeigniter:显示RAW查询结果

时间:2011-08-30 16:17:46

标签: codeigniter profiler

有没有办法获得使用Active Record创建的查询结果而不使用print_r()这样的东西?

我知道Profiler in Codeigniter但是我需要一些能够显示查询输出的东西,而不仅仅是查询本身(就像分析器一样)。

谢谢!

1 个答案:

答案 0 :(得分:4)

我开始研究这个并最终扩展了探查器类以打印查询结果。就像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;
    }
}

这与profiler类中的原始函数非常相似,我添加了这个(+变量名的更改):

<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

希望这有帮助!