Re: Searching My Database

From: Date: Wed, 29 Sep 2004 16:03:24 +0000
Subject: Re: Searching My Database
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-198203@lists.php.net to get a copy of this message
Harlequin wrote:
Morning everyone. I've read around the subject of searching and although using the FULLTEXT capability of MySQL might be the proper way of doing this I feel it's somewhat limited and have decided to use a simple select procedure. I'm sure there's a better way of doing this, as I'm quite new to MySQL and even newer to searches, However - here's my conundrum: I'm declaring variables at the top of my query like so:
      Code:
      $WorkPermit          == '" . $_POST["WorkPermit"] .  "';
And then execute the query like so:
      Code:
      SELECT * FROM MembersData
      WHERE Work_Permit_Rqd
      LIKE '$WorkPermit'
But I have many other fields that the searcher can use. What do I do if they leave this field blank...? I appreciate that I should be using the MATCH function but I'm not entirely happy with the way it searches. What I need to do is actually omit a field (s) from the search if the value the searcher submitted was NULL. For example: Search field X and Y and Z and if x or Y are null continue... Am I explaining this OK...? Any suggestions gratefully received.
Hello Michael! Try this script if you want (just a quick-and-dirty hack): [code] <?php if (!empty($_POST)) { $where = ''; foreach ($_POST['search'] as $field => $value) {
    if ('' == $value) {
      continue;
    }
    $where .= $field.' LIKE "'.$value.'" AND ';
} if ('' != $where) {
      $where = substr($where, 0, -5);
      print 'SELECT * FROM Work_Permit_Rqd WHERE '.$where;
} } ?> <form action="<?php print $_SERVER['PHP_SELF']; ?>" method="post"> <input type="text" name="search[field1]"><br> <input type="text" name="search[field2]"><br> <input type="text" name="search[field3]"><br> <input type="submit" name="submit" value="submit"> </form> [/code] Daniel

« previous php.general (#198203) next »