Re: Multi search function (help)
| From: | David Robley | Date: | Wed, 26 May 2004 22:01:53 +0000 |
| Subject: | Re: Multi search function (help) | ||
| References: | 1 2 3 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-34562@lists.php.net to get a copy of this message | ||
Nabil wrote:
> "David Robley" <robleyd@ozemail.com.au> wrote in message
> news:20040526113904.54450.qmail@pb1.pair.com...
>> Nabil wrote:
>>
>> > hi all....
>> >
>> > Is there a way to condition your search:
>> >
>> > -I have a form for four text boxes for search my Mysql...
>> > -I don't want to write 4 conditions and for SQL statements incase he
>> > decided not to search with all keywords (fields)
>> > - I have by example : name, lastname , nickname and phone form...
>> > I need a way to select my records even one or more field were null
>> > (searching only on of the above)
>> >
>> > because the following SQL will generate null result
>> >
>> >
>> > $name=$_POST['naame'];
>> > $lastname=$_POST['lastname'];
>> > $nickname=$_POST['nickname'];
>> > $m_date=$_POST['m_dateY'];
>> > echo $name.$lastname.$nickname.$m_date;
>> >
>> > "SELECT id, name , lastname , m_date from users
>> > where
>> > name like binary '%$name%' and lastname like binary '%$lastname%'
>> > and
>> > nickname like binary '%$nickname%' and m_date= YEAR('$m_date')
>> > order
> by
>> > id ASC ") or die(mysql_error());
>> >
>> >
>> >
>> > Thanks in advanced
>>
>> Use isset to test whether the POST values are set and only include in the
>> query if there is a value.
> thanks .. but my question is not for isset... i m thinking consider that i
> have 10 search fields... if i have to do a combination then i need a day
> to right the various SQL statements
So do something like:
$query = "SELECT id, name , lastname , m_date from users where 1 ";
if (isset($_POST['name'])) {
$query .= "AND name like binary '%{$-POST['name']}%' ";
}
if (isset($_POST['lastname'])) {
$query .= "AND name like binary '%{$_post['lastname']}%' ";
}
//etc etc
$query .= " order by id ASC ";
$sql = mysql_query($query) or die(mysql_error());
--
David Robley
Only cosmetologists give make-up exams.