title: Cloning a PHP PDO recordset object to count the rows returned by a select statement
body: <p>I'm refactoring part of a PHP application which uses adodb as db library, in order to start using PDO.</p>
  
  <p>I need something which provides me the number of rows included in a select recordset, something that can easily replace the old <code>$rs-&gt;RecordCount()</code> adodb method that I used before. It should work for sqlite3.</p>
  
  <p>I can't simply re-execute the query (or re-execute the query using a <code>select count(*)</code> ) because I can't change so much the original application, the application calls in many parts a function <code>get_num_rows($rs)</code> (which contains just <code>$rs-&gt;RecordCount()</code> ) and I can just change the content of that function.</p>
  
  <p>I tried cloning the recordset object and counting the records fetched:</p>
  
  <pre><code>function get_num_rows($rs)
  {
      $rs_copy = clone $rs;
      return (count($rs_copy-&gt;fetchAll()));
  }
  </code></pre>
  
  <p>but it doesn't work, because <code>$rs_copy-&gt;fetchAll()</code> returns me <code>false</code>. I can't do it on the original recordset because later in the applicaiton I need to fetch it again and I think there is no way in PDO sqlite to reuse the recordset (correct me if I'm wrong).</p>
  
  <p>Do you have any solutions?</p>