473,404 Members | 2,137 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

How to include 'and' operator in the following select statements?

135 100+
Guy's,

The sitution is that I have 3 opions or select statements which the user selects and i need to include the AND operator in them, just in case the user selects all three options, how do I do this, I need to include the following in this code below($clause .= " and ";). Currently I'm getting an syntax error when the user selects more then one option:

Expand|Select|Wrap|Line Numbers
  1. $query = "select * from ".$table;
  2. $clause = "";
  3.  
  4. if ($keywords1 ne '') {
  5.     $clause .= " where ".$FORM{'category1'}." like '%" . $keywords1 . "%' ";
  6. }
  7.  
  8. if ($keywords2 ne '') {
  9.     $clause .= " where ".$FORM{'category2'}." like '%" . $keywords2 . "%' ";
  10. }
  11.  
  12. if ($keywords3 ne '') {
  13.     $clause .= " where ".$FORM{'category3'}." like '%" . $keywords3 . "%' ";
  14. }
  15.  
Jul 13 '07 #1
1 1063
miller
1,089 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. my %where = (
  2.     $FORM{'category1'} => $keywords1,
  3.     $FORM{'category2'} => $keywords2,
  4.     $FORM{'category3'} => $keywords3,
  5. );
  6.  
  7. # Build Where Clauses
  8. my @clause = ();
  9. my @params = ();
  10. while (my ($key, $val) = each %where) {
  11.     next if $val eq '';
  12.     push @clause, qq{$key LIKE CONCAT('%', ?, '%')};
  13.     push @params, $val;
  14. }
  15. my $where = @clause ? " WHERE " . join(' AND ', @clause) : '';
  16.  
  17. my $sth = $dbh->prepare("SELECT * FROM " . $table . $where);
  18. $sth->execute(@params) or die $dbh->errstr;
  19.  
- Miller
Jul 13 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: wEEdpEckEr | last post by:
Hi, here's the deal: I have a config.php file in which I have set a few var's, to use in the whole site. E.g.: $db_host = "localhost"; Then I also have a class, which is supposed to...
0
by: Bil Click | last post by:
I am coding classic ASP pages in Visual Studio .Net 2003. I file called counties_option.asp that just has a list of options: <OPTION VALUE="030">ANSON</OPTION> <OPTION VALUE="040">ASHE</OPTION>...
1
by: Bil Click | last post by:
I am coding classic ASP pages in Visual Studio .Net 2003. I have a file called counties_option.asp that just has a list of options: <OPTION VALUE="030">ANSON</OPTION> <OPTION...
4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
7
by: mark | last post by:
Access 2000: I creating a report that has a record source built by the user who selects the WHERE values. An example is: SELECT * FROM CHARGELOG WHERE STDATE Between #10/27/2003# And...
5
by: David Mathog | last post by:
One thing that can make porting C code from one platform to another miserable is #include. In particular, the need to either place the path to an included file within the #include statement or to...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
51
by: Kuku | last post by:
What is the difference between a reference and a pointer?
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.