phpcodeigniteractiverecordwhere-clausebetween

Construct a CodeIgniter WHERE condition with the value BETWEEN two column values


Here is my model

public function getDetails($ip)
{
    $this->db->select('country_code, country_name');
    $this->db->where("'2323' BETWEEN begin_ip_num AND end_ip_num");
    return $this->db->get('ip')->row();
}

I get this error:

Unknown column ''2323'' in 'where clause'

SELECT `country_code`, `country_name` FROM (`ip`) WHERE `2130706433` BETWEEN begin_ip_num AND end_ip_num

Where am I going wrong?


Solution

  • 2323 is evaluated as a column name, and begin_ip_num and end_ip_num as values. You must do the opposite, like this :

    <?php
    public function getDetails($ip)
        {
            $this->db->select('country_code, country_name');
            $this->db->where("begin_ip_num <= 2323 AND end_ip_num >= 2323");
            return $this->db->get('ip')->row();
        }
    

    See MySQL Reference on 'BETWEEN' for how this should be used.