I have a sql statement in my model,
I then say
$query = $this->db->query($sql, array(fields, fields1);
if ($query) {
return true:
} else {
use get_compiled_select()
to retrieve query instead of replace it
To display the query string:
print_r($this->db->last_query());
To display the query result:
print_r($query);
The Profiler Class will display benchmark results, queries you have run, and $_POST data at the bottom of your pages. To enable the profiler place the following line anywhere within your Controller methods:
$this->output->enable_profiler(TRUE);
Profiling user guide: https://www.codeigniter.com/user_guide/general/profiling.html
Add this line right after the query you want to print.
Example:
$query = $this->db->query('SELECT * FROM table WHERE condition');
//Add this line.
var_dump($this->db->last_query());
exit();
or
echo $this->db->last_query();
I try to @Chumillas's answer and @chhameed's answer, but it not work,because the sql is wrong.So I found new approach,like this:
echo $sql; flush(); exit;
into before return $sql;
_compile_select
function of DB_active_rec.php
Neither last_query()
or get_compiled_select()
works for me, so a slight change of pedro's code works for me just fine. Do not include ->get()
in your build, this must be before the ->get()
echo $this->EE->db->_compile_select();
I had exactly the same problem and found the solution eventually. My query runs like:
$result = mysqli_query($link,'SELECT * FROM clients WHERE ' . $sql_where . ' AND ' . $sql_where2 . ' ORDER BY acconame ASC ');
In order to display the sql command, all I had to do was to create a variable ($resultstring) with the exact same content as my query and then echo it, like this:<?php echo $resultstring = 'SELECT * FROM clients WHERE ' . $sql_where . ' AND ' . $sql_where2 . ' ORDER BY acconame ASC '; ?>
It works!