Theming a custom data table from php code text format with Drupal 7 I have a working block of php code that runs inside a Drupal 7 Page:

&lt;?php
$my_database= array(
  'database' =&gt; 'mydb',
  'username' =&gt; 'xxxxxx', // assuming this is necessary
  'password' =&gt; 'xxxxxx', // assuming this is necessary
  'host' =&gt; 'localhost', // assumes localhost
  'driver' =&gt; 'mysql', // replace with your database driver
);
Database::addConnectionInfo('database_db', 'default', $my_database);
db_set_active('database_db');
$sql = "SELECT tcol1, tcol2 FROM mytable LIMIT 10";

$result = db_query($sql);
$header = array('Col1', 'Col2');
$rows = array();
if ($result) {
  while ($row = $result-&gt;fetchAssoc()) {
    $row= array($row['tcol1'],$row['tcol2']);
  $rows[] = $row;
  }
}
db_set_active();
print_r($rows);
$output= theme('table', $header, $rows);
?&gt;


This piece of code works fine and print_r() prints all the data I asked for. 
The odd thing is that the theming fails totally, showing nothing. I have done some experimentation:

    $output= theme('table', $header, $rows);
    echo theme('table', $header, $rows);
    print theme('table', $header, $rows);


but all of them fail, showing nothing or disrupting drupal globally.
How can I show my data in a nice table? I want to avoid writing html code and instead rely on Drupal theming.
The format of arguments to theme functions changed in Drupal 7, instead of

$output = theme('table', $header, $rows);


you need to use

$output = theme('table', array('header' =&gt; $header, 'rows' =&gt; $rows));


or, using a render array,

$build = array(
  '#theme' =&gt; 'table',
  '#header' =&gt; $header,
  '#rows' =&gt; $rows,
);
$output = drupal_render($build);