Jump to content

Searching for not equal


JayNo

Recommended Posts

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:

[code]$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');[/code]

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

Thanks.
Link to comment
Share on other sites

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

[code]$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');[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.