Welcome Guest, Not a member yet? Register   Sign In
$this->db->like
#1

[eluser]Yash[/eluser]
Hi,

I want to search on strict base.I mean if i provide "php" it should only search php and not %php%
How I can achieve this?

Code:
$this->db->like($somefield,'somestring');
$this->db->from('table');
Thank You
#2

[eluser]Référencement Google[/eluser]
Then use a WHERE query and not a LIKE.
Code:
$this->db->where($somefield,'somestring');
#3

[eluser]Yash[/eluser]
can it generate more than one result?
#4

[eluser]Référencement Google[/eluser]
The query will return all results that correspond to the WHERE string, but using where assume that your field contains only the searched string, you can not use a WHERE query for searching in a piece of text for example.

Assuming you work with a model, you'll have to return it with something like:
Code:
function find_string($string = FALSE)
{
    $this->db->where('somefield', $string);
    $query = $this->db->get('table');
    return $query->result();
}

Also, I am not sure I get what you really want to do, it will be good if you explain more because if you want to search a piece of text in a field for an exact match, of course don't use a WHERE query.

If you have problems, please post your code so we can better help you.
#5

[eluser]arume[/eluser]
My solution:

Code:
$this->db->where('somefield LIKE '.$this->db->escape($fieldvalue), null, false);

By the way, maybe you must take care to not allow wildcards (in mysql "%" and "_"). Unfortunately CI don't care about that (in the AR LIKE method) nor provide a method to backslash it (as far as I know). So I wrote a function to do it in a mysql_helper.

Code:
function mysql_escape_like($str)
{
  return addcslashes($str, '%_');
}

So, the code will be:

Code:
$this->db->where('somefield LIKE '.mysql_escape_like($this->db->escape($fieldvalue)), null, false);
#6

[eluser]Yash[/eluser]
that's really nice code arume. and thank you Too Pixel.

I want to search a exact string in a field.
Actually I'm searching text in a field and giving id for that field.So I can have many ids for given text.

My code

Code:
//$cat   - search string
$temp="";
$this->db->where('Cat', $cat);
$query = $this->db->get('table');
foreach($query->result() as $row)
{
   $temp[] = $row->ID;
}
return $temp;
#7

[eluser]arume[/eluser]
OK. So, why do you need LIKE?
#8

[eluser]Yash[/eluser]
no no..in starting I thought it can be achieve only by like.

I've solved the prob.
#9

[eluser]arume[/eluser]
Ah. OK.

You must use "=" when you can. Use "LIKE" only to match substrings or to match strings ignoring case letters.




Theme © iAndrew 2016 - Forum software by © MyBB