Welcome Guest, Not a member yet? Register   Sign In
Help with CI active record and like statement
#1

[eluser]CroNiX[/eluser]
Using active record, is it possible to generate a like statement like this (using 'LOWER' in the where)? Or should I just query using my own sql statement?

SELECT name FROM users WHERE LOWER(name) LIKE '$some_var%';

Ive been doing:
Code:
return $this->db->select('name')
                ->like('name', $search_name, 'after')
                ->get('users')
                ->result_array();
Thanks,
CroNiX
#2

[eluser]John_Betong[/eluser]
 
Whoops: misread the question.

Please ignore Sad
 
 
 
edit:
 
 
Ok, I will try again.
 
Try Google and search for "MySql like case-sensitive" for a complete explanation.

In a nutshell then using LIKE maybe case-sensitive depending on the COLLATION of your tables.
 
 
 
#3

[eluser]Aken[/eluser]
You can put LOWER(name) in place of your table name and it should generate just like the example you added. Oh, and FYI - in your code example, you have 'after' selected in your third parameter of the like() function. This will generate 'some_var%'. If you want to search both ways, you can omit the third param, or change it to 'both'.

Code:
$search_name = 'search_name';

$this->db->select('name')
    ->like('LOWER(name)', $search_name)
    ->get('users');

// GENERATES: SELECT `name` FROM (`users`) WHERE LOWER(name) LIKE '%search_name%'
#4

[eluser]CroNiX[/eluser]
[quote author="Aken" date="1252656094"]You can put LOWER(name) in place of your table name and it should generate just like the example you added. Oh, and FYI - in your code example, you have 'after' selected in your third parameter of the like() function. This will generate 'some_var%'. If you want to search both ways, you can omit the third param, or change it to 'both'.

Code:
$search_name = 'search_name';

$this->db->select('name')
    ->like('LOWER(name)', $search_name)
    ->get('users');

// GENERATES: SELECT `name` FROM (`users`) WHERE LOWER(name) LIKE '%search_name%'
[/quote]
Yes, Im using 'after' because its for an autocompleter. Thanks for the info, I swear I tried that like statement you posted...maybe I did something else wrong but its working now.
#5

[eluser]CroNiX[/eluser]
[quote author="John_Betong" date="1252652832"] 
Whoops: misread the question.

Please ignore Sad
 
 
 
edit:
 
 
Ok, I will try again.
 
Try Google and search for "MySql like case-sensitive" for a complete explanation.

In a nutshell then using LIKE maybe case-sensitive depending on the COLLATION of your tables.
 
 
 [/quote]
Yes I know...thats why Im forcing it to lowercase because the search string is lowercase and Im working with multiple databases in multiple formats. Just easier to force to lower...
#6

[eluser]Aken[/eluser]
[quote author="CroNiX" date="1252660226"]Yes, Im using 'after' because its for an autocompleter. Thanks for the info, I swear I tried that like statement you posted...maybe I did something else wrong but its working now.[/quote]
Gotcha Smile Just making sure since your example had both % surrounding the search term. Good luck with your application!




Theme © iAndrew 2016 - Forum software by © MyBB