How to print an SQL statement in a codeigniter model

I have a sql statement in my model,

Then i say

$query = $this->db->query($sql, array(fields, fields1); if ($query) { return true: } else { echo "failed"; return false; } 

My query always fails, how do I get php to print the exact sql statement sent to my database? And display this in my php view, page

+89
sql php codeigniter
May 26 '11 at 16:43
source share
13 answers

To display the query string:

 $this->db->last_query(); 

To display the result of a query:

 print_r($query); 



The Profiler class will display test results, your queries, and the $ _POST data at the bottom of your pages. To enable the profiler, place the following line anywhere on your controller’s methods:

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

Profiling User Guide: https://www.codeigniter.com/user_guide/general/profiling.html

+80
May 26 '11 at 16:49
source share

You can use this:

 $this->db->last_query(); 

"Returns the last requested query (query string, not result).

Reff: https://www.codeigniter.com/userguide3/database/helpers.html

+224
May 27 '11 at 11:59
source share

You can display SQL Server ActiveRecord:

Before running the request:

 $this->db->_compile_select(); 

And after its launch:

 $this->db->last_query(); 
+37
May 26 '11 at 21:02
source share

if you need a quick test according to your request this works fine for me

 echo $this->db->last_query(); die; 
+16
Jan 13 '16 at 2:05
source share

After unsuccessfully using _compiled_select() or get_compiled_select() I just printed the db object, and you can see the query there in the queries property.

Try it yourself:

 var_dump( $this->db ); 

If you know that you have only one request, you can print it directly:

 echo $this->db->queries[0]; 
+11
Apr 02 '14 at 19:26
source share

You can just use this at the end.

 echo $this->db->last_query(); 
+9
Aug 01 '15 at 10:10
source share

There is a new public method get_compiled_select , which can print a request before running it. _compile_select now protected, so it cannot be used.

 echo $this->db->get_compiled_select(); // before $this->db->get(); 
+8
May 16 '13 at 6:22
source share

last_query() or get_compiled_select() does not work for me, so a small change to the Pedro code works very well for me. Do not include ->get() in your assembly, this should be before ->get() get ()

  echo $this->EE->db->_compile_select(); 
+2
07 Oct '13 at 13:34 on
source share

I am trying to answer @Chumillas and @chhameed answer, but it does not work because sql is incorrect. So I found a new approach , like this:

  • Insert echo $sql; flush(); exit; echo $sql; flush(); exit; in return $sql; _compile_select function DB_active_rec.php
+1
Oct 26 '13 at 14:42
source share

Add this line immediately after the request you want to print.

Example:

$ query = $ this-> db-> query ('SELECT * FROM table WHERE condition');

// Add this line.

print ($ this-> db-> last_query ());

exit();

or

echo $ this-> db-> last_query ();

+1
Nov 07 '18 at 3:10
source share

I had exactly the same problem and ended up finding a solution. My request is executed as follows:

 $result = mysqli_query($link,'SELECT * FROM clients WHERE ' . $sql_where . ' AND ' . $sql_where2 . ' ORDER BY acconame ASC '); 

To display the sql command, all I had to do was create a variable ($ resultstring) with the same contents as my query, and then repeat it like this: <?php echo $resultstring = 'SELECT * FROM clients WHERE ' . $sql_where . ' AND ' . $sql_where2 . ' ORDER BY acconame ASC '; ?> <?php echo $resultstring = 'SELECT * FROM clients WHERE ' . $sql_where . ' AND ' . $sql_where2 . ' ORDER BY acconame ASC '; ?>

It works!

0
Mar 15 '16 at 9:43
source share

I read all the answers here, but cannot get

 echo $this->db->get_compiled_select(); 

to work, he gave me an error, for example,

Calling the protected method CI_DB_active_record :: _ compile_select () from the context of the 'Welcome'in controller on line xx

So, I removed protected from the next line from the file \system\database\DB_active_rec.php and worked

 protected function _compile_select($select_override = FALSE) 
-one
Jun 24 '15 at 10:07
source share

use get_compiled_select() to get the request instead of replacing it

-one
Jan 26 '18 at 11:18
source share



All Articles