0

I'm using CodeIgniter to select data from a MySQL table based on if data from 1 column's string length is greater than 1.

Code:

        $this->db->order_by('time_stamp', 'desc');
        $this->db->where('strlen(user_categrory)>1');//I know this code does not work but that's what I wanted to achieve
        $data['record']=$this->db->select('username, user_category, email, source, status, test_group, time_stamp, referrer_email')->get('user');

Any advice on how to get this done? Thanks a ton.

user3192948
  • 231
  • 2
  • 22

2 Answers2

0

Pass false as where()'s third argument, and CodeIgnite won't escape the string from the first argument https://ellislab.com/codeigniter/user-guide/database/active_record.html#select. You can then use MySQL's LENGTH() function:

$this->db->where('LENGTH(user_categrory)>1', null, false);

Also, you might need to use CHAR_LENGTH() instead of LENGTH(), as LENGTH() is in bytes, while CHAR_LENGTH() is actual characters.

kfriend
  • 2,584
  • 1
  • 19
  • 15
0

strlen function not work in mysql please use LENGTH function for string length,please follow this code try it.

    $this->db->select('username, user_category, email, source, status,  test_group, time_stamp, referrer_email');
    $this->db->order_by('time_stamp', 'desc');
    $this->db->where('length(user_categrory)>1');
    $data['record']=$this->db->get('user');
Kack
  • 86
  • 2