title: PHP create MySQL query with conditions if/elseif/else
body: <p>I'm new to PHP &amp; MySQL and was trying to use php to create query for MySQL below is the current state of the code and it's still WIP but the if/elseif structure already gives the desired output which varies according to data input, which for example could be as follows</p>
  
  <pre><code>"SELECT * FROM tuntikortti WHERE PVM BETWEEN '2014-09-11' AND '2014-09-27' AND Henkilo =
   'Van' AND Tyonumero = '123456' AND Toiminto = '123';"
  </code></pre>
  
  <p>I've confirmed that the query line itself works in MySQL but I would need to somehow get it inside the MySQL_query($con, <em>The query here</em>).</p>
  
  <p>So how do I get the php output of whole if/elseif query line build in variable or similar to be used?</p>
  
  <pre><code>&lt;?php
  include("db_connect.php");
  
  $pvm1       = $_POST['pvm1'];
  $pvm2       = $_POST['pvm2'];
  $henkilo    = $_POST['henkilo'];
  $tyonumero  = $_POST['tyonumero'];
  $toiminto   = $_POST['toiminto'];
  
  /* MySQL query line build, beginning */
  ECHO "SELECT * FROM tuntikortti WHERE ";
  
      /* PVM */
      if(!empty($pvm1) &amp;&amp; !empty($pvm2)){
          echo "PVM BETWEEN '". $pvm1 ."' AND '". $pvm2 ."'";
      } elseif(!empty($pvm1)) {
          echo "PVM = '". $pvm1 ."'";
      };
  
      /* Henkilo */
      if(!empty($henkilo) &amp;&amp; !empty($pvm1)){
          echo " AND Henkilo = '". $henkilo ."'";
      } elseif(!empty($henkilo)) {
          echo " Henkilo = '". $henkilo ."'";
      };
  
      /* Tyonumero */
      if(!empty($tyonumero) &amp;&amp; (!empty($pvm1) || !empty($henkilo))){
              echo " AND Tyonumero = '". $tyonumero ."'";
          } elseif(!empty($tyonumero)) {
              echo " Tyonumero = '". $tyonumero ."'";
          };
  
      /* Toiminto */
      if(!empty($toiminto) &amp;&amp; (!empty($tyonumero) || !empty($pvm1) || !empty($henkilo))){
          echo "  AND Toiminto = '". $toiminto ."'";
      } elseif(!empty($toiminto)) {
              echo " Toiminto = '". $toiminto ."'";
          };
  
      echo ";";
  
      $query = mysqli_query($sql,$con);
  
  echo "&lt;table border='1'&gt;
  &lt;tr&gt;
  &lt;th&gt;TID&lt;/th&gt;
  &lt;th&gt;PVM&lt;/th&gt;
  &lt;th&gt;Henkilo&lt;/th&gt;
  &lt;th&gt;Työnumero&lt;/th&gt;
  &lt;th&gt;Toiminto&lt;/th&gt;
  &lt;th&gt;Tunnit&lt;/th&gt;
  &lt;th&gt;Selite&lt;/th&gt;
  &lt;/tr&gt;";
  
  while($row = mysqli_fetch_array($query)) {
    echo "&lt;tr&gt;";
    echo "&lt;td&gt;" . $row['TID'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['PVM'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['Henkilo'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['Tyonumero'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['Toiminto'] . "&lt;/td&gt;";
    echo "&lt;td&gt;" . $row['Tunnit'] . "&lt;/td&gt;";
    echo "&lt;td class='selitetyyli'&gt;" . $row['Selite'] . "&lt;/td&gt;";
    echo "&lt;/tr&gt;";
  }
  
  echo "&lt;/table&gt;";
  
  include("db_close_connection.php");
      ?&gt;
  </code></pre>