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!
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.
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.
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.
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.
Before the query runs:
$this->db->_compile_select();
And after it has run:
$this->db->last_query();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With