Jump to content


Photo

Searching for not equal


  • Please log in to reply
1 reply to this topic

#1 JayNo

JayNo
  • New Members
  • Pip
  • Newbie
  • 4 posts
  • LocationToronto

Posted 27 February 2006 - 04:47 PM

I'm having trouble searching for things from my database.

I have a drop down list of clients, eg..
- all clients <initially selected>
- Asahi
- Budweiser
- Corona
- Heineken

if the user doesn't select anything I want to search for everything in the database. If a client is selected i want it to search for data from only that client. The only problem is that there is about 20 other fields that have areas like this.

Anyone have an idea as to how to set up a search function to do this.

My code right now looks like this:

$client = $_POST['client'];
if ($client == 'All'){
  $clientCompare = '!=';
} else {
  $clientCompare = '=';
}
mysql_select_db($mysqldatabase, $mysqlconnection);
$testQuery = " SELECT * FROM tbl_test1 WHERE col_clientName $clientCompare $client
";
$testResult = mysql_query($testQuery) or die('Error, query failed');

Obviously, i will need other conditions for the other form data, but just testing right now.

Thanks.

#2 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 27 February 2006 - 07:23 PM

Why make MySQL evaluate a condition that doesn't have semantic meaning? I always do it something like this:

$client = $_POST['client'];
if ($client != 'All') $whereclause .= 'col_clientName=' . $client;
// add more conditions

if ($whereclause) $whereclause = "WHERE " . $whereclause;
mysql_select_db($mysqldatabase, $mysqlconnection);
$testQuery = "SELECT * FROM tbl_test1 $whereclause";
$testResult = mysql_query($testQuery) or die('Error, query failed');





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users