0

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?

2
  • $this->db->where("'2323' BETWEEN begin_ip_num AND end_ip_num"); Is actually $this->db->where("$ip BETWEEN begin_ip_num AND end_ip_num"); Commented Jan 16, 2011 at 12:28
  • where you are wrong? everywhere...you are misunderstanding the between() function! hope the answer on your previous question will clear things up for you. Commented Jan 16, 2011 at 12:33

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

I think you meant:

$this->db->where("$ip BETWEEN begin_ip_num AND end_ip_num");

Because $ip is coming from your model function something you want to check against.

Comments

0

Upon testing with CodeIgniter 3.1.11, here is a battery of active record attempts and rendered queries assuming that the passed in $num is a string type value:

Quality Syntax SQL
->where(
sprintf(
'%s BETWEEN %s AND %s',
$this->db->escape($num),
$this->db->escape_identifiers('from_num'),
$this->db->escape_identifiers('to_num')
)
)
WHERE '2323' BETWEEN `from_num` AND `to_num`
->where(
[
$this->db->escape($num) . ' >=' => $this->db->escape_identifiers('from_num'),
$this->db->escape($num) . ' <=' => $this->db->escape_identifiers('to_num'),
],
null,
false
)
WHERE '2323' >= `from_num`
AND '2323' <= `to_num`
->where("$num BETWEEN", 'from_num AND to_num')
WHERE 2323 BETWEEN 'from_num AND to_num'
💀
->where_between($num, ['from_num', 'to_num'])
undefined method where_between()

It must be stated that if your value is a numeric (integer or float type) value, then it doesn't need any quoting. If your column names are not reserved keywords for your database dialect, then they do not need quoting. Assuming these conditions are true, you can entertain the following:

Quality Syntax SQL
->where("from_num <= $num AND to_num >= $num")
WHERE `from_num` <= 2323 AND `to_num` >= 2323
⚠️
->where("$num BETWEEN from_num AND to_num")
WHERE 2323 BETWEEN `from_num` AND to_num
⚠️
->where("$num BETWEEN", 'from_num AND to_num', false)
WHERE 2323 BETWEEN from_num AND to_num

⭐ is my recommendation, ✅ means correct and fully quoted, ⚠️ means it will work but not fully quoted, ❌ means it is incorrect, 💀 means it will emit an error

CodeIgniter BETWEEN-related answers of mine:

A similarly formatted table of CodeIgniter method calls can be found at:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.