title: getting column names in mssql pdo
body: <p>I'm building an HTML table from some MS SQL PDO queries. Or trying to. The first roadblock I've hit is that I cannot get the column names of a particular table. Found <a href="https://stackoverflow.com/questions/5428262/php-pdo-get-the-columns-name-of-a-table">here</a>, I've tried to us the solution</p>
  
  <pre><code>function getColumnNames(){ 
  
  $sql = "select column_name from information_schema.columns where table_name = 'myTable'";
  #$sql = 'SHOW COLUMNS FROM ' . $this-&gt;table; 
  
  $stmt = $this-&gt;connection-&gt;prepare($sql); //this is the line that triggers the error
  
  try {     
      if($stmt-&gt;execute()){ 
          $raw_column_data = $stmt-&gt;fetchAll(PDO::FETCH_ASSOC); 
  
          foreach($raw_column_data as $outer_key =&gt; $array){ 
              foreach($array as $inner_key =&gt; $value){ 
                          if (!(int)$inner_key){ 
                              $this-&gt;column_names[] = $value; 
                          } 
              } 
          } 
          } 
          return $this-&gt;column_names; 
      } catch (Exception $e){ 
              return $e-&gt;getMessage(); //return exception 
      }         
  }  
  
  getColumnNames();
  </code></pre>
  
  <p>got the error:</p>
  
  <pre><code>Fatal error: Using $this when not in object context
  </code></pre>
  
  <p>Whereas this (from the same SO post)</p>
  
  <pre><code>$q = $dbh-&gt;prepare("DESCRIBE username");
  $q-&gt;execute();
  $table_fields = $q-&gt;fetchAll(PDO::FETCH_COLUMN);
  print_r($table_fields);
  </code></pre>
  
  <p>yielded the error:</p>
  
  <pre><code>Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 2812 General SQL Server error: Check messages from the SQL Server [2812] (severity 16) [(null)]'
  </code></pre>
  
  <p>I'm just trying to get the names of the columns so I can loop through and get the values of each of their rows. How can I accomplish this? Thanks</p>