phpcodeigniteractiverecordnumericquoting

How to prevent CodeIgniter's active record from automatically wrapping a numeric strings in quotes


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?


Solution

  • 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);