title: How to use variables in WHERE clause for a SQL SELECT query
body: <p>My SQL SELECT query has 3 variables set by a HTML form, however these variables can be null:</p>
  
  <pre><code>   $suburb = (isset($_POST['suburb'])) ? $_POST['suburb'] : '';
     $postcode = (isset($_POST['postcode'])) ? $_POST['postcode'] : '';
     $state = (isset($_POST['state'])) ? $_POST['state'] : '';
  </code></pre>
  
  <p>When all 3 variables are entered in the form the SELECT query processes fine (due to the AND's in the WHERE clause). But when one or more variables are blank, the SELECT query errors as it is looking for a NULL value in the databse.</p>
  
  <p>What I want it to do is when a variable is null, I don't want a where clause for it</p>
  
  <p>Query:</p>
  
  <pre><code>$sql = &lt;&lt;&lt;SQL
  SELECT 
      cs.customerRefId,
      cc.firstName,
      cc.lastName,
      cc.email,
      cc.phone,
      cc.mobile,
      cs.seekingAddressSuburb, 
      cs.seekingAddressPostcode, 
      cs.seekingAddressState
  FROM 
      customer_seeking cs
  LEFT JOIN
      customer_contact cc ON cc.customerRefId = cs.customerRefId
  WHERE
  cs.seekingAddressSuburb = '$suburb' AND
  cs.seekingAddressPostcode = '$postcode' AND
  cs.seekingAddressState = '$state'
  SQL;  
  
  
  if(!$result = $db-&gt;query($sql)){
      die('There was an error running the query [' . $db-&gt;error . ']');
  }
  </code></pre>
  
  <p>For example if only the state variable is defined (cs.seekingAddressSuburb = '$suburb' AND
  cs.seekingAddressPostcode = '$postcode' AND are removed from the WHERE clause):</p>
  
  <pre><code>$sql = &lt;&lt;&lt;SQL
  SELECT 
      cs.customerRefId,
      cc.firstName,
      cc.lastName,
      cc.email,
      cc.phone,
      cc.mobile,
      cs.seekingAddressSuburb, 
      cs.seekingAddressPostcode, 
      cs.seekingAddressState
  FROM 
      customer_seeking cs
  LEFT JOIN
      customer_contact cc ON cc.customerRefId = cs.customerRefId
  WHERE
  cs.seekingAddressState = '$state'
  SQL;  
  </code></pre>