title: Create a dynamic mysql query using php variables
body: <p>I have an html table that loads everything in a mySQL database table.  I have dropdowns that relate to columns of that mySQL table - when the user selects one of the dropdowns it uses AJAX to query the database.</p>
  <p>I need to figure out how to build the query dynamically because sometimes the dropdowns will be empty (i.e. they don't want to filter by that column).</p>
  <p>What is the best way to do this?</p>
  <p>Currently I have something like this:</p>
  <pre><code>    $stationFilter = $_GET['station'];
      $verticalFilter = $_GET['vertical'];
      $creativeFilter = $_GET['creative'];
      $weekFilter = $_GET['week'];    
      
      $result = mysql_query(&quot;SELECT * FROM tableName WHERE STATION_NETWORK = '$stationFilter' AND VERTICAL = '$verticalFilter' AND CREATIVE = '$creativeFilter'  AND WK = '$weekFilter'&quot;);   
      $data = array();
      while ($row = mysql_fetch_row($result) )
          {
          $data[] = $row;
          }   
      $finalarray['rowdata'] = $data;
  </code></pre>
  <p>Which you can imagine doesn't work because if any of those fields are empty - the query fails (or returns nothing, rather).</p>
  <p>Obviously creating such a 'static' query like that really makes it difficult if certain variables are empty.</p>
  <p>What is the best way to dynamically create that query so that it only enters the ones that are not empty get added to the query so it can successfully complete and display the appropriate data?</p>