Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

active record in codeigniter automatically adds quotes around where clause values

I've tried reading other posts on stackoverflow and also checked the active record documentation for ci, but i can't seem to find the answer to my question

I have the following logic in my model:

    $query = $this->db->get_where('categories', array('parent_id' => $category_id));

the sql this generates as per the last_query() method is:

SELECT * FROM (categories) WHERE parent_id = '8'

I need to remove the quotes around the number 8. How would I do that?

I've tried using the select statement and passing false as the second parm. So for example:

    $this->db->select('*', false);
    $this->db->from('categories');
    $this->db->where('parent_id=',$category_id);

But that didn't really change much. Any suggestions? Thank you

like image 718
dot Avatar asked Jan 27 '13 14:01

dot


1 Answers

By default, CodeIgniter tries to predict the data type in your comparison, and use the appropriate SQL syntax accordingly. If your query is using single quotes, it might indicate that $category_id is being treated as a string rather than an integer. What happens if you try:

$this->db->select('*');
$this->db->from('categories');
$this->db->where('parent_id', (int) $category_id);

Alternatively, you can construct your own WHERE statement manually:

$this->db->where('parent_id = ' . (int) $category_id);
like image 79
hohner Avatar answered Sep 24 '22 00:09

hohner