Escape the order by query in codeigniter - php

Here is the SQL query run :
SELECT * FROM (`news`) WHERE `country` IS NULL AND `region` IS NULL ORDER BY IFNULL(update_date, `create_date)` DESC
And you may notice that the create_date has some formatting error, I would like to disable the escape but even I add false after the order_by function it has no effect. How to fix it? Thanks a lot
$this->db->select('*');
$this->db->from('news');
$this->db->where($data);
$this->db->order_by('IFNULL(update_date,create_date)', 'DESC', false);
$query = $this->db->get();
return $query->result_array();

Use below code:
$this->db->_protect_identifiers = FALSE;
$this->db->order_by('IFNULL(update_date,create_date)', 'DESC', false);
$this->db->_protect_identifiers = TRUE;

add this line on top of your db select method.
$this->db->_protect_identifiers = FALSE;

Just to update, trying to make a sql select with multiplication to work on C.I. 3.10
$this->db->order_by("results.result * 1", 'ASC', FALSE);
Just adding , FALSE to order_by resolved because $this->db->_protect_identifiers was triggering an error on C.I. 3.10

Related

Codeigniter 3x Query Builder class seems to be generating extra segments to my query

I am using the latest version of the Codeigniter framework for PHP. Here is my code.
public function get_user_by_username( $username ) {
$this->db->where( 'username', strtolower( $username ) );
$q = $this->db->get( 'users', 1 );
foreach( $q->result() as $row ) {
return $row;
}
return NULL
}
I called the last_query() method to get the query that was generating the problem and this pops up messing up the entire query. I didn't code this. Codeigniter is generating this on its own.
SELECT * FROM `users`
WHERE
`serial` IS NULL AND
`password` IS NULL AND
`username` = '[username]'
LIMIT 1
I need Codeigniter to generate this instead as I expected.
SELECT * FROM `users` WHERE `username` = '[username]' LIMIT 1
I'm just now starting to code with Codeigniter 3x after using version 2x for the last couple of years. Thanks in advance.
Look into $this->db->flush_cache(); and $this->db->reset_query() before your query to clear all pre-set elements.
Most likely, serial and pass have been previously set on $this->db.
For more info, check the CodeIgniter's docs

error when passing a date to where clause

So this is odd and hopefully there is a simple answer. I have a SQL query that used a date as the first parameter in a where() clause. But for some reason CodeIginter appears to be trying to add in extra escape characters,
Here is my code
$this->db->select("*");
$this->db->from("equip_reserve");
$this->db->where('equip_list_id = '.$equip_list_id);
$this->db->where('"'. date("Y-m-d H:i",strtotime($startdate)) .'" between date and returndate');
Here is the result:
SELECT * FROM 'equip_reserve' WHERE 'equip_list_id' = 5 AND "2016-02-29 '14:20'" between 'date' and 'returndate'
As you can see the date after the AND clause is causing this statement to fail.
Any suggestions on how to fix it?
I ended up fixing it by just passing the SQL directly to CodeIgniter instead of using their helper functions. In case anyone ever has this problem.
$st = date("Y-m-d H:i",strtotime($startdate));
$sql = "SELECT * FROM `equip_reserve` WHERE ? between `date` AND `returndate` and `equip_list_id` = ?";
$query = $this->db->query(
$sql,
array($star,$equip_list_id)
);
return $query->result();
Fixed!

How to write the given query in CodeIgniter

I want to write the given query in codeigniter , how can I do so?
SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`);
I gone through codeigniter's where_in() but i'm getting wrong result , here what I have done,
$this->db->select('*');
$this->db->from(USER);
$this->db->where_in('id', 'select `provider_id` from `services`');
$query = $this->db->get();
return $query->num_rows();
it is returning 0, while the query above, when run directly in phpmyadmin, returns 1 record.
You can use this simple way $this->db->query('your query');
Please check this post, hope it will help you Using Mysql WHERE IN clause in codeigniter
You can pass direct queries in where class by passing second parameter as NULL and third parameter as FALSE
<?php
$where = 'id IN (select `provider_id` from `services`)';
$this->db->select('*');
$this->db->from(USER);
$this->db->where($where, NULL, FALSE);
$query = $this->db->get();
return $query->num_rows();
Use $this->db->query();
$query=$this->db->query("SELECT * FROM `user` WHERE `id` IN (select `provider_id` from `services`)");
$num=$query->num_rows();
$result= $query->result();

Laravel Order By certain value

I'd like for one of my relationships to return ordering by a certain value as follows:
return $this->hasMany('Photo', 'owner_id')->where('active', '=', '1')->where('visible', '=', '1')->orderByRaw("(`id` = ?) DESC", array($showphoto));
But the above code returns this :
select * from `photos` where `photos`.`owner_id` = '4' and `active` = '1' and `visible` = '1' order by (`id` = '') DESC
How do I get the $showphoto variable to be in the order by?
It won't show you that it was injected since you are not currently triggering the query.
When you will chain the get() method to it, it will "inject" the $showphoto and execute the query.
I am not aware of a way you can see the full query including the values of the parameters before actually executing the query.

Codeigniter subquery exists

Im trying to do a subquery with
$this->db->where(" EXISTS (SELECT * FROM myTable)");
But it doesnt work, the output of this is: myquery + WHERE 'EXISTS (SELECT * FROM myTable);
That quote before the EXISTS makes the query unresolvable!
Does anyone knows how to solve it?
Thanks!
please remove space before and after EXISTS keyword.that does not display any error.
$this->db->where("EXISTS(SELECT * FROM myTable)");
Maybe you could try to set the escape to false by using
$this->db->where(" EXISTS (SELECT * FROM myTable)", null, false);
This is the snippet of where() in DB_active_rec.php
public function where($key, $value = NULL, $escape = TRUE)
Just try this.
Instead of using 'where' clause, please write down the complete query string & execute the query using $this->db->query();
$qry_string= $yourquery . "WHERE EXISTS (SELECT * FROM myTable)";
$this->db->query($qry_string);

Categories