title: How to prepare a MySQL query using a variable length parameter list in PHP
body: <p>I've got a simple PHP script which queries a mysql database for basic user info based on the data fetched from an HTML form. </p>
  
  <pre><code>&lt;?php
      $age = $_POST['age'];
      $gender = $_POST['gender'];
      $dbc = mysqli_connect('localhost', 'root', 'abc123', 'mydb')
      $query = "SELECT * FROM users WHERE AGE='$age' AND GENDER='$gender'";
      $result = mysqli_query($dbc, $query) or die('Querying the db failed');
      mysqli_close($dbc);
  ?&gt;
  </code></pre>
  
  <p>The problem is that the user doesn't always have to pick a gender or age and as a result the query doesn't always succeed. In other words I'm looking for something like this:</p>
  
  <pre><code>//(In pseudocode)
  if (only $age exists) then:
      $query = "SELECT * FROM users WHERE AGE='$age'
  if (only $gender exists) then:
      $query = "SELECT * FROM users WHERE GENDER='$gender'
  if (both $age and $gender exist) then:
      $query = "SELECT * FROM users WHERE AGE='$age' AND GENDER='$gender'";
  otherwise: 
      $query = "SELECT * FROM users"
  </code></pre>
  
  <p>How should I do this? (in the actual script there are way more variables than just age and gender).</p>