Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Concatenate search fields

Posted on 2004-09-15
3
Medium Priority
?
385 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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 Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

649 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