title: How can I get column names from PDOStatement?
body: <p>I've recently built a class for automatic paging and ordering of query results, that works with PDO.</p>
  
  <p>Here's how I retrieve the data from a MySQL table:</p>
  
  <pre><code>$this-&gt;query = "SELECT _id, name, price, creationDate, isPublished FROM fruits";
  $this-&gt;fetch_type = PDO::FETCH_ASSOC;
  $this-&gt;result = $db-&gt;query($this-&gt;query);
  $this-&gt;rows = $this-&gt;result-&gt;fetchAll($this-&gt;fetch_type);
  $this-&gt;columns = empty($this-&gt;rows) ? array() : array_keys($this-&gt;rows[0]);
  </code></pre>
  
  <p>This way I can easily store the column names inside an array (that's exactly what I need):</p>
  
  <pre><code>var_dump($this-&gt;columns):
  
  array
    0 =&gt; string '_id' (length=3)
    1 =&gt; string 'name' (length=4)
    2 =&gt; string 'price' (length=5)
    3 =&gt; string 'creationDate' (length=12)
    4 =&gt; string 'isPublished' (length=11)
  </code></pre>
  
  <p>However, my method doesn't work if the fetch type is <code>PDO::FETCH_OBJ</code>, since I'm no more working with 2D arrays, but with objects inside arrays:</p>
  
  <pre><code>var_dump($this-&gt;rows):
  
  array
   0 =&gt; 
  object(stdClass)[11]
    public '_id' =&gt; string '1' (length=1)
    public 'name' =&gt; string 'apple' (length=5)
    public 'price' =&gt; string '26.00' (length=5)
    public 'creationDate' =&gt; string '0000-00-00 00:00:00' (length=19)
    public 'isPublished' =&gt; string '1' (length=1)
   1 =&gt; 
  object(stdClass)[12]
    public '_id' =&gt; string '2' (length=1)
    public 'name' =&gt; string 'banana' (length=11)
    public 'price' =&gt; string '15.00' (length=5)
    public 'creationDate' =&gt; string '0000-00-00 00:00:00' (length=19)
    public 'isPublished' =&gt; string '1' (length=1)
   2 =&gt; 
  object(stdClass)[13]
    public '_id' =&gt; string '3' (length=1)
    public 'name' =&gt; string 'orange' (length=6)
    public 'price' =&gt; string '12.12' (length=5)
    public 'creationDate' =&gt; string '0000-00-00 00:00:00' (length=19)
    public 'isPublished' =&gt; string '1' (length=1)
  
    etc...
  </code></pre>
  
  <p>So, how can I get column names from a result like this above?</p>