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:
between()function! hope the answer on your previous question will clear things up for you.