1

I use ajax search in my site. The user can search through homes. I want to enable user to do an exact search with exactly the keywords he wants. But the my problem is I don't know from what the user types how many keywords I can search. This is my query:

$query = "Select a.*,c.name as agent_name,d.country_name,e.state_name,g.city from #__osrs_properties as a"
                                ." inner join #__osrs_agents as c on c.id = a.agent_id"
                                ." inner join #__osrs_types as f on f.id = a.pro_type"
                                ." inner join #__osrs_categories as j on j.id = a.category_id"
                                ." inner join #__osrs_countries as d on d.id = a.country"
                                ." inner join #__osrs_states as e on e.id = a.state"
                                ." inner join #__osrs_cities as g on g.id = a.city"
                                ." where a.approved = '1' and a.published = '1' and a.category_id = '$category->id' and (";
                        //

                        $newKeyword = explode(" ",$keyword);    
                        $query1="j.category_name like '%$newKeyword[0]%' and f.type_name like '%$newKeyword[1]%' and  g.city like '%$newKeyword[2]%'";

it works but when user only types 3 keywords and no more.

1
  • Shouldn't you use MySQL OR instead of AND? Commented Sep 15, 2015 at 11:44

4 Answers 4

1

use OR instead of AND

$query1 = "j.category_name like '%$newKeyword[0]%' OR f.type_name like '%$newKeyword[1]%' OR g.city like '%$newKeyword[2]%'";

I hope it will help you.

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

2 Comments

thanks the code i was written is ok. my problem is i don't know user type how many keywords. maybe 3 or maybe 5
then you can check the count after explode then use for-loop your will get your answer as you want. Hope it will help you also.
0

You will need to somehow know what the user's are supplying. E.g. what fields you want to match. Right now, you assume the keywords are in a certain order, but is that guaranteed? I suspect not if you can't be sure the user will supply all 3.

I would suggest breaking up the keywords into fields that can be determined and apply them individually to the query. Otherwise it will be impossible to know which of the 3 keywords was supplied.

Also you will want to sanitize the user supplied values to prevent malicious injection.

Comments

0

For getting exact result you should use regular expression in query

$query1="j.category_name REGEXP '$newKeyword[0]' and f.type_name REGEXP '$newKeyword[1]' and g.city REGEXP '$newKeyword[2]'";

Comments

0

The best option is not to try to re-invent the wheel and just use FULLTEXT index in MySQL. Then just prepend all your keywords with + (plus sign) and search by normal SELECT . MySQL will take care of your keywords mix regardless of order and regardless of field size in your database.

Comments

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.