Codeigniter: Display RAW query results
Is there a way to get the result of a query that was created with Active Record without using stuff like print_r() ?
I know about the Profiler in Codeigniter but I need something that also displays the output of the query, not only the query itself开发者_Python百科 (as the profiler does).
Thanks!
I started to look into this and ended up extending the profiler class to also print the query result. Like jondavidjohn I am a little confused about what you really want, but hopefully this comes close. What you will have to do is to create a file called MY_Profiler.php in application/libraries and then paste the following code into it:
<?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;"> '.$this->CI->lang->line('profiler_queries').' </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 JOIN', 'ORDER BY', 'GROUP BY', 'LIMIT', 'INSERT', 'INTO', 'VALUES', 'UPDATE', 'OR ', 'HAVING', 'OFFSET', 'NOT IN', 'IN', 'LIKE', 'NOT 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;"> '.$this->CI->lang->line('profiler_database').': '.$db->database.' '.$this->CI->lang->line('profiler_queries').': '.count($db->queries).' '.$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." </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;
}
}
This is very similar to the original function in the profiler class, what I added was this (+ a change of variable name):
<td style='padding:5px; color:#000;font-weight:normal;background-color:#ddd;'><pre>".print_r($db->query($query)->result_array(), true)."</pre></td>
By changing this code, you can style how the result appears. Of course, since this is just an extension of the profiler, you need to make sure the profiler is enabled:
$this->output->enable_profiler(TRUE);
The profiler will then print a new column with the result in it, which looks like this:
Hopefully this helps!
精彩评论