Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make like() method in db active record in CodeIgniter case-insensitive?

e.g. If I have these variables:

$variable1 = "ABCDEFG";
$variable2 = "AbCDefG";

and stuff in db like these:

ABCDEFG
IJKLMNO
PQRSTUV
...

And if I try to use like() method like this:

function get_users()
{

    $q = $this->db->select('*')
                  ->from('users')
                  ->order_by('created asc')
                  ->like('username', $variable1)
                  ->get();
    return $q;

}

The result will be OK and find the ABCDEFG record from database.

However, if I pass $variable2 with Uppercase and Lowercase the result will be none:

function get_users()
{

    $q = $this->db->select('*')
                  ->from('users')
                  ->order_by('created asc')
                  ->like('username', $variable2)
                  ->get();
    return $q;

}

, which is wrong , because I need to ignore if it is lowercase or uppercase.

How to solve this?

Btw. my db collation is utf8_general_ci

like image 881
Derfder Avatar asked Dec 07 '22 10:12

Derfder


1 Answers

Try this one:

function get_users()
{

    $q = $this->db->select('*')
                  ->from('users')
                  ->order_by('created asc')
                  ->like('LOWER(username)', strtolower($variable2))
                  ->get();
    return $q;

}
like image 130
Code Prank Avatar answered May 10 '23 23:05

Code Prank