Codeigniter:显示RAW查询结果

标签 codeigniter profiler

是否有一种方法可以获取使用 Active Record 创建的查询的结果,而无需使用 print_r() 之类的东西?

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

谢谢!

最佳答案

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

这与分析器类中的原始函数非常相似,我添加的是这个(+变量名称的更改):

<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

希望这有帮助!

关于Codeigniter:显示RAW查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7246387/

相关文章:

php - 数据从模型到 Controller 再返回到模型

linux - 仅当线程运行时才向线程发送信号

sql-server - SqlProfiler 捕获加密查询。这是对的吗?

java - hsqldb java 分析示例程序

php - Codeigniter消息: Undefined variable: query

mysql - codeigniter 中 where 子句中的列 'id' 不明确

php - codeigniter:从其他配置文件访问配置变量?

debugging - Dynamics CRM - 必须多次卸载并安装 Profiler 才能在插件注册工具中进行调试

java - 如何在分析器的同时启动 Java 应用程序?

codeigniter,在特定页面上强制使用 ssl