title: How to handle dynamic number of parameters in querystring when building REST api?
body: <p>When building a RESTful api that deals with a resource, which can be queried by a dynamic set of parameters, what is the best way to build the query to the database?</p>
  
  <p>Say the resource is a book, and the possible parameters are:</p>
  
  <pre><code>author, year, publisher, pages, rating
  </code></pre>
  
  <p>and you can build a query with any number of parameters and any combination like:</p>
  
  <pre><code>/books?rating=2
  </code></pre>
  
  <p>or</p>
  
  <pre><code>/books?author=james&amp;year=2001&amp;rating=4
  </code></pre>
  
  <p>or</p>
  
  <pre><code>/books?year=2010&amp;publisher=greatbooks&amp;pages=100&amp;rating=5
  </code></pre>
  
  <p>Whats regarded as a good way of transforming this dynamic set of parameters into a database query? </p>
  
  <p>Creating a LOT of if else statements like:</p>
  
  <pre><code>if( isset($_GET['rating'] &amp;&amp; isset($_GET['author']) ) {
  
      //Do query based on these parameters here...
  
  }
  </code></pre>
  
  <p>or</p>
  
  <pre><code>if( isset($_GET['author'] &amp;&amp; isset($_GET['year']) &amp;&amp; isset($_GET['publisher']) ) {
  
      //Do query based on these parameters here...
  
  }
  </code></pre>
  
  <p>etc etc etc ...</p>
  
  <p>Or setting all the varibles and then using LIKE instead of '=' in the query like this:</p>
  
  <pre><code>if(!empty($_GET['author'])) {
      $author = $_GET['author'];
  } else {
      $author = '%';
  }
  </code></pre>
  
  <p>and then</p>
  
  <pre><code>SELECT * FROM books WHERE author LIKE $author ... and so on
  </code></pre>
  
  <p>Or is there some other way of handling this?</p>