Shouldn't the NOT be right before the LIKE: SELECT * FROM books WHERE title LIKE '%php%' AND title NOT LIKE '%perl%'
Duncan -- Duncan Salada | Titan | www.titan.com/testeval Email: [EMAIL PROTECTED] | Voice: 301-925-3222x375 | Fax: 301-925-3216 > -----Original Message----- > From: Jonathan David Edwin Wright [mailto:[EMAIL PROTECTED]] > Sent: Thursday, April 25, 2002 12:01 PM > To: [EMAIL PROTECTED] > Subject: Not Liked! > > > Hiya, > > I'm writing a bit of code to add a bit of extra functionality > to a basic search on a web site. > > When searching for items in a library, I want to be able to > select what I want and don't want to be searched for, i.e. > +linux -beginner searches for all books on Linux, but doesn't > show any for beginners. > > I've got the code to generate the SQL statement: > > $commands = explode(" ", $searchstring); > $sql = "SELECT * FROM ".$searchtable." WHERE "; > > for ($i=0;$i<count($commands);$i++) { > if (preg_match("/^([\+\-]?)(\S+)$/", $commands[$i], $preg)) { > if ($i > 0) { > switch($preg[1]) { > case "+": > $sql .= " AND "; > break; > > case "-": > $sql .= " AND NOT "; > break; > > default: > $sql .= " OR "; > break; > } > } > > if ($i == 0 && $preg[1] == "-") { > $sql .= " NOT "; > } > > $search = array("/%/", "/_/", "/\*/", "/\?/"); > $replace = array("\\%", "\\_", "%", "_"); > $preg[2] = preg_replace($search, $replace, $preg[2], -1); > > $sql .= "title LIKE '%".$preg[2]."%'"; > } > } > > Which turns '+php -perl' into: > > SELECT * FROM books WHERE title LIKE '%php%' AND NOT title > LIKE '%perl%' > > The problem I get is that the query doesn't seam to work. One > of the books in the table is 'Professional PHP Programming', > but, while 'php' will pick it up, and 'php perl' as well, > 'php -perl' won't! > > Surly it should work! :-S > > Thanks for your help, > > -- > Jonathan Wright > [EMAIL PROTECTED] > [EMAIL PROTECTED] > -- > > > --------------------------------------------------------------------- > Before posting, please check: > http://www.mysql.com/manual.php (the manual) > http://lists.mysql.com/ (the list archive) > > To request this thread, e-mail <[EMAIL PROTECTED]> > To unsubscribe, e-mail > <[EMAIL PROTECTED]> > Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php > --------------------------------------------------------------------- Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail <[EMAIL PROTECTED]> To unsubscribe, e-mail <[EMAIL PROTECTED]> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php