Solved

Concatenate search fields

Posted on 2004-09-15
3
378 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now