Search From Different Tables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
yoursanjay
Forum Newbie
Posts: 17
Joined: Sat Feb 23, 2008 12:21 pm

Search From Different Tables

Post by yoursanjay »

I have 2 fields, Keywords & Type. I have to search from 2 different tables knb_faq & knb_solution as per the keywords & type.
Here is my mysql query :

Code: Select all

 
$keyword=$_GET['keyword'];
$type=$_GET['type'];
$srch=mysql_query("select knb_faq.*,knb_solution.* from knb_faq,knb_solution where (knb_faq.solution LIKE '%$keyword%' or knb_faq.keywords LIKE '%$keyword%' or knb_faq.subject LIKE '%$keyword%'and knb_faq.type='$type') or
 (knb_solution.solution LIKE '%$keyword%' or knb_solution.keywords LIKE '%$keyword%' or knb_faq.solution LIKE '%$keyword%' and knb_solution.type='$type')") or die (mysql_error());
 
 
There is no error msg but my problem is that for this query I am not getting result perfectly. The search is occured but it is not searching from both the tables according to Keyword & Type.
Is there any wrong in the sql query? If so, Please tell me the right syntax.
Last edited by Benjamin on Mon May 11, 2009 3:39 am, edited 1 time in total.
Reason: Changed code type from text to php.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Search From Different Tables

Post by requinix »

You can't search two unrelated tables at the same time like that. Do two queries: one for the FAQ table, one for the solutions table.

Also, AND has higher precedence than OR. "a OR b AND c" really means "a OR (b AND c)". If you don't want that then put parentheses where you want them.
Post Reply