Solved

Concatenate search fields

Posted on 2004-09-15
3
381 Views
Last Modified: 2012-06-27
I'm building my web site using php and mysql.  I'm converting an exiting website from ASP/MS ACCESS.  I'm having a problem with a simple select statement that utilises two drop down select boxes and one single keyword box.  The results from the drop down boxes (County and License fields) work well.  The single keyword box, which searches several fileds (Company, Title, Description), produces syntax errors.  This is the code I'm trying to write on the results page:

$query_rsSearch = sprintf("SELECT * FROM projects WHERE County LIKE '%%%s%%' AND License LIKE '%%%s%%' AND Company+','+Title+','+Description+' LIKE '%%%s%%'", $varcounty__rsSearch,$varlicense__rsSearch,$varkeyword__rsSearch);

Any suggestions on how to correct this world be appreciated.
0
Comment
Question by:grnt
  • 2
3 Comments
 
LVL 4

Expert Comment

by:chipple
ID: 12071642
It should be:

$query_rsSearch = sprintf("SELECT * FROM projects WHERE County LIKE '%%%s%%' AND License LIKE '%%%s%%' AND Company+','+Title+','+Description LIKE '%%%s%%'", $varcounty__rsSearch,$varlicense__rsSearch,$varkeyword__rsSearch);

Also, you should be careful of SQL injection and use mysql_escape_string on all data input by the user.

$query_rsSearch = sprintf("SELECT * FROM projects WHERE County LIKE '%%%s%%' AND License LIKE '%%%s%%' AND Company+','+Title+','+Description LIKE '%%%s%%'", mysql_escape_string($varcounty__rsSearch),mysql_escape_string($varlicense__rsSearch),mysql_escape_string($varkeyword__rsSearch));

Good luck.
0
 

Author Comment

by:grnt
ID: 12071832
Thanks for the input.  Good news, the syntax error message has gone but when I enter a keword in the keyword box, it produces no results.  The County and License fields still produce results but the keyword field draws a blank.  Any further suggestions?
0
 
LVL 4

Accepted Solution

by:
chipple earned 100 total points
ID: 12071873
Looks correct to me, it all depends on whether you have data matching the criteria. For sure entering more than one keyword in your keyword box won't produce the expected result since you're doing an exact match (not matching for each keyword).
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question