Re: Database query

From: Date: Tue, 18 Dec 2001 16:34:15 +0000
Subject: Re: Database query
References: 1  Groups: php.db 
Request: Send a blank email to php-db+get-15088@lists.php.net to get a copy of this message
You are getting this result because you have all of those ORs in your statement. If any of them are true it will execute the query. If location finds a match ('$location' = location) it will execute the query regardless of whether discipline was empty or simply did not match. It is generally a bad idea to use logic like this in a query if there are ambiguities. You can get the results you are looking for by using the following query: $sql = "SELECT * FROM vacancy WHERE '$related_discipline' LIKE related_discipline and '$location' LIKE location ORDER BY ID LIMIT $limitvalue1, $limit"; Then make the value of your first option (the one which indicates no selection) in the html form = "%". Now if a selection is made using both variables and the selection does not find any relevant rows you will get an empty result. Fred <Bzdpltd@aol.com> wrote in message news:a0.1f464cd0.2950956a@aol.com... > Hi group, > > I am still getting to grips with PHP and MySQL and have a question that hopefully someone can easily answer: > > I have a search page which consists of two drop down menus, the user has 3 ways of finding information. > > The first is by selecting a location and a discipline which would look in the database and return any entries with the data in the two seperate fields. > > The second is where they can select the location only, and this will return all entries with that location > > The third is where they can select the discipline only, and again this will return all entries with that discipline. > > The problem I have is when the user selects the location and a discipline which should return no results as it is not an exact match it is returning results as it contains part of the information. > > My sql query is as follows: $sql = "SELECT * FROM vacancy WHERE ('$related_discipline' = related_discipline and '$location' = location) or ('$related_discipline' = related_discipline) or ('$location' = location) ORDER BY ID LIMIT $limitvalue1, $limit"; > > Could someone tell me what I need to change? > > Again help is always appreciated. >

« previous php.db (#15088) next »