?
Solved

Concatenate search fields

Posted on 2004-09-15
3
Medium Priority
?
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

In this article, we’ll look at how to deploy ProxySQL.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

752 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