Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting raw SQL Queries in CodeIgniter 1.7

I'm trying to debug some code in my first serious CodeIgniter app and I can't seem to find where I can simply get the raw SQL that my ActiveRecord code just generated.

    $where  = 'DAY(`datetime_start`) = '. date('d',$day) .' AND ';     $where .= 'MONTH(`datetime_start`) = '. date('m',$day) .'';      $this->db->from('events')->where($where);     $result = $this->db->get(); 

Thanks for the help!

like image 968
bkorte Avatar asked Oct 02 '09 04:10

bkorte


People also ask

How write raw SQL query in CodeIgniter?

Syntax for Raw Query MethodUse query() method of CodeIgniter 4. $db = db_connect(); OR $db = \Config\Database::connect(); It will connect with default connection group of application. Now, by using $db we can write and run queries.

Is raw SQL faster than ORM?

There is little research on which technique is faster. Intuitively, Raw SQL should be faster than Eloquent ORM, but exactly how much faster needs to be researched. In particular, when one uses Raw SQL over Eloquent ORM, one makes a trade-off between ease of development, and performance.

What is a raw query in SQL?

Raw SQL queries are useful if the query you want can't be expressed using LINQ. Raw SQL queries are also used if using a LINQ query is resulting in an inefficient SQL query. Raw SQL queries can return regular entity types or keyless entity types that are part of your model.

Does CodeIgniter prevent SQL injection?

SQL injection is an attack made on database query. In PHP, we are use mysql_real_escape_string() function to prevent this along with other techniques but CodeIgniter provides inbuilt functions and libraries to prevent this.


1 Answers

Before the query runs:

$this->db->_compile_select();  

And after it has run:

$this->db->last_query(); 
like image 198
hipertracker Avatar answered Sep 21 '22 01:09

hipertracker