0

I am searching a keyword in DB that is Bootjack Fire and Rescue Foundation As there is single space after a word, my query is `

WHERE `title` LIKE  '%Bootjack Fire and  Rescue Foundation%'

Why are there two space after the and keyword in the search title as you can see in the query?

add query

$keyword = 'Bootjack Fire and Rescue Foundation';

$this->db->select('stores.id,store_title,store_category_id,store_sub_category_id,store_sub_category_type_id,sub_category_fourth,store_link');
$this->db->from('stores');
$this->db->join('users','users.id=stores.user_id');
$this->db->join('store_category','stores.store_category_id=store_category.id');

$where="`store_title` LIKE  '%".$keyword."%'";
$this->db->where($where);
$this->db->where('store_status', 1);
$this->db->where('store_type', 2);
$this->db->where('store_category.status', 1);
$this->db->order_by('store_title','ASC');
$this->db->order_by('store_category_id');
8
  • update the question to include the code that creates the query Commented Jul 19, 2016 at 10:31
  • I have updated my query Commented Jul 19, 2016 at 10:34
  • 1
    I don't see anything in your code that would add an extra space after and, unless your db class does it. Is that a class of your own or a 3rd-party library? Commented Jul 19, 2016 at 10:56
  • No its simple search that add one more space after and keyword. And I am really shocked Commented Jul 19, 2016 at 11:00
  • Show the code for $this->db->where(), because that's what seems to be doing it. Commented Jul 19, 2016 at 11:04

2 Answers 2

1

I think is not possible directly in the query.

You can transform your keyword to repair any wrong data.

I hope it helps you.

Sign up to request clarification or add additional context in comments.

3 Comments

I can do this but I want exact match and this is the first time that i have seen this issue that taking double spaces after and word
The keyword variable doesn't have wrong data. He's claiming that the space gets added when it gets put into the query.
Ok @Barmar, I did not understand the question correctly
1

You can convert multiple space into a single one then use the same like clause.

$where="replace(replace(replace(store_title,' ','<>'),'><',''),'<>',' ') LIKE  '% replace(replace(replace(".$keyword".,' ','<>'),'><',''),'<>',' ') %'";

considering < and > symbol will not appear in that column value, otherwise need to use some other symbols. I have used this in SQL-SERVER should works in Mysql also.

You can remove multiple spaces in php like below...

$keyword=preg_replace('/\s+/', ' ',$keyword);

then use the simple sql as...

$where="store_title LIKE '% ".$keyword" %'";

2 Comments

oops o/p is same "WHERE replace(replace(replace(store_title,' ','<>'),'><',''),' <> ',' ') LIKE '%Bootjack Fire and re%' " as you can see double spaces after and word
The extra space is in the query, not the table data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.