title: Convert PDO recordset to JSON in PHP
body: <p>Im using PHP and I need a way to convert an entire recordset to a JSON string.</p>
  
  <p>While searching Stack Overflow I found this solution that works:</p>
  
  <pre><code>function recordSetToJson($mysql_result) {
    $rs = array();
    while($rs[] = mysql_fetch_assoc($mysql_result)) {
      // you don´t really need to do anything here.
    }
    return json_encode($rs);
  }
  </code></pre>
  
  <p>The problem with this code is that I found that the function <code>mysql_fetch_assoc()</code> is deprecated in PHP 5.5.0. Another thing is that im using PDO to connect to my database.</p>
  
  <p>Given the circunstances above, what would be the best solution to convert a PDO recordset to JSON? I want it to work at later versions of PHP too.</p>