【问题标题】:Codeigniter: Display RAW query resultsCodeigniter:显示 RAW 查询结果
【发布时间】:2011-08-30 16:17:45
【问题描述】:

有没有办法在不使用 print_r() 之类的东西的情况下获取使用 Active Record 创建的查询的结果?

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

谢谢!

【问题讨论】:

  • 类似:SELECT * FROM cars 将输出:+---------+------+ | id_car |车牌| +---------+------------------+ | 123 | SBX-08-YXG | +---------+-----------------+
  • "Something like..." 根本不是特定的,当您说 RAW 结果时,您必须有一些标准或示例的 RAW 结果来尝试模拟。你说的RAW是指mysql命令行工具的输出吗?

标签: codeigniter profiler


【解决方案1】:

我开始研究这个并最终扩展了探查器类来打印查询结果。像 jondavidjohn 一样,我对你真正想要的东西有点困惑,但希望这能接近。您需要做的是在 application/libraries 中创建一个名为 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>

通过更改此代码,您可以设置结果显示方式的样式。当然,由于这只是 profiler 的一个扩展,所以你需要确保 profiler 是启用的:

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

然后,分析器将打印一个包含结果的新列,如下所示:

希望这会有所帮助!

【讨论】:

  • 谢谢,正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
相关资源
最近更新 更多