Re: Wildcard

From: Date: Thu, 11 Jul 2002 09:26:39 +0000
Subject: Re: Wildcard
Groups: php.general 
Request: Send a blank email to php-general+get-107081@lists.php.net to get a copy of this message
>I’m trying to figure out how to do a search trough a MySQL db using LIKE >or = but the thing is that I have 3 select boxes from where to choose >the search terms. Can I use something like a wildcard instead of making >several IF statements like this? > >SELECT * FROM table_name WHERE col1 = value1 AND col2 = value2 AND col3 >= % > >% goes for an unselected select box (default value = %) in case the user >doesn’t want to make an *advanced* search, so it fetches all rows which >does contains values 1 & 2. Here is what I would do, if I was doing what I think you're doing, which I'm not at all sure of... <?php $query = "select col1, col2, ... from table_name where 1 = 1 "; if (isset($value1)){ $query .= " and col1 = $value1 "; } if (isset($value2)){ $query .= " and col2 = $value2 "; } if (isset($value3)){ $query .= " and col3 = $value3 "; } mysql_query($query) or error_log(mysql_error()); ?> The where 1 = 1 technique is a sort of "yeast" to allow all the subsequent parts to start with " and ..." If you change to " OR ... " then you need " where 1 = 0 " Think about it or work through an example to see why :-) -- Like Music? http://l-i-e.com/artists.htm Off-Topic: What is the moral equivalent of 'cat' in Windows?

« previous php.general (#107081) next »