CodeIgniter Forums
How to echo statement of query with Active Record - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to echo statement of query with Active Record (/showthread.php?tid=64379)



How to echo statement of query with Active Record - SDir - 02-15-2016

Hello, 

how can I echo the statement of a query when I use code as this?
PHP Code:
$this->db->from($this->table); 

thank you


RE: How to echo statement of query with Active Record - XMadMax - 02-15-2016

(02-15-2016, 02:05 AM)SDir Wrote: Hello, 

how can I echo the statement of a query when I use code as this?
PHP Code:
$this->db->from($this->table); 

thank you

Use :

PHP Code:
echo $this->db->last_query(); 

Other usefull query utilities :
http://www.codeigniter.com/user_guide/database/db_driver_reference.html?highlight=insert_string#CI_DB_driver::insert_string


RE: How to echo statement of query with Active Record - SDir - 02-15-2016

(02-15-2016, 02:21 AM)XMadMax Wrote:
(02-15-2016, 02:05 AM)SDir Wrote: Hello, 

how can I echo the statement of a query when I use code as this?
PHP Code:
$this->db->from($this->table); 

thank you

Use :

PHP Code:
echo $this->db->last_query(); 

Other usefull query utilities :
http://www.codeigniter.com/user_guide/database/db_driver_reference.html?highlight=insert_string#CI_DB_driver::insert_string


Thank You for your answer.  But I can't use last_query() because of this unsolved probem (working with Oracle): http://forum.codeigniter.com/thread-64340.html

For example: if I write this code, I get no output from last_query(): 
PHP Code:
    $this->db->from($this->table);    
    
$this->db->last_query();  //no output with Oracle (but with MySql works fine) 

So, I'm looking for a different method to print the statement.



Thank You


RE: How to echo statement of query with Active Record - Avenirer - 02-15-2016

A query is generated only if... you generate it...

The from() ("$this->db->from($this->table);") method is not generating a query by itself. You must also have a result generating function like, for example, a "get()" method. So, if you want to have the last_query(), you must have:

PHP Code:
$this->db->select('*');
$this->db->from($this->table);
$this->db->get();
echo 
$this->db->last_query(); 

...or, even simpler...:

PHP Code:
$this->db->get($this->table);
echo 
$this->db->last_query(); 



RE: How to echo statement of query with Active Record - mwhitney - 02-15-2016

...or use get_compiled_select() https://codeigniter.com/user_guide/database/query_builder.html#CI_DB_query_builder::get_compiled_select

PHP Code:
$this->db->from($this->table); 
$sql $this->db->get_compiled_select($this->tablefalse);