title: PHP PDO timeout
body: <p>I have a certain functionality in a PHP page that retrieves through AJAX some data from MySQL.
  It is related to obtaining a list of items grouped by categories by giving the categories needed. When the function is called with 1 category, it takes almost a minute to return result and with more than 1, the server gives 500 Internal Server Error and in the log there is: </p>
  
  <pre><code>[warn] mod_fcgid: read data timeout in 60 seconds
  [error] Premature end of script headers: search.php
  </code></pre>
  
  <p>So obviously the query takes too long to execute (the error occurs right at the </p>
  
  <pre><code>$query-&gt;execute();
  </code></pre>
  
  <p>line - all lines before and no lines after it get executed in the case with more than 1 category which gives 500 ISE).</p>
  
  <p>The problem is that when I run the query through phpMyAdmin by hand, it says </p>
  
  <pre><code>Query took 0.8030 sec
  </code></pre>
  
  <p><strong>What can be causing that huge difference in execution time (less then a second to more than a minute)?</strong></p>
  
  <p>Note: because there is a random number of categories queried for, I build dynamically the categories part of the WHERE statement using all three columns making up the composite key for a category:</p>
  
  <pre><code>$whereCategories = '(';
      for($i=0;$i&lt;count($categories);$i++)
      {
              $whereCategories.= ($i==0?'':' OR ').'(id1=:id1'.$i.
                      ' AND id2=:id2'.$i.
                      ' AND id3=:id3'.$i.')';
      }
      $whereCategories.= ')';
  </code></pre>
  
  <p>And then attach it to the query:</p>
  
  <pre><code>$query = $this-&gt;connection-&gt;prepare("SELECT ... WHERE ... ".
           ($whereCategories=="()"?"":" AND ".$whereCategories).
           " ... ");
  </code></pre>
  
  <p>and bind all the values:</p>
  
  <pre><code>for($i=0;$i&lt;count($categories);$i++)
      {
          $query-&gt;bindValue(':id1'.$i, $categories[$i]['id1']);
          $query-&gt;bindValue(':id2'.$i, $categories[$i]['id2']);
          $query-&gt;bindValue(':id3'.$i, $categories[$i]['id3']);
      }
  
  //code runs up to here
      $result = $query-&gt;execute();
  //code here doesn't run
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  I fixed the performance issue by putting all of the categories values directly in the query string, instead of using <code>$query-&gt;bindValue();</code>. However, I'd still appreciate it if somebody can explain why the multiple value bindings were working so terribly slow? (even for 2 categories - that's just 6 bindings.)</p>