phpcodeigniteractiverecordsql-likecase-insensitive

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


Solution

  • Try this one:

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