Return to Snippet

Revision: 7624
at August 1, 2008 12:02 by berkes


Initial Code
function helpers_database_render_table($table_name) {
  $columns = $header = $rows = array();

  $result = db_queryd('SHOW columns in {%s}', $table_name);
  while ($column = db_fetch_object($result)) {
    $header[] = t('@columnname (@columntype)', array('@columnname' => $column->Field, '@columntype' => $column->Type));
    $columns[] = $column->Field;
  }

  $result = pager_query('SELECT * FROM {%s}', 50, 0, NULL, $table_name);
  while ($record = db_fetch_object($result)) {
    foreach($columns as $column) {
      //@TODO: include spans with title=fullresult on large results, and add ellipses in that case.
      $row[$column] = drupal_substr($record->$column, 0, 36);
    }
    $rows[] = $row;
  }

  $count = db_result(db_query("SELECT count(%s) FROM {%s}", $columns[0], $table_name));

  $caption = t('SELECT * FROM {%table_name} resulted in %count results', array('%table_name' => $table_name, '%count' => $count));

  return theme('table', $header, $rows, array(), $caption) . theme('pager');
}

Initial URL

                                

Initial Description
Drupal helper function to debug a table. Returns the contents and some explanation of a database table in a rendered format (HTML table).

**NOTE** You REALLY do not want to put this function behind any kind of menu_callback and/or on other pages. Use it for debugging only: Because we return any contents, regardless of permissions and so forth, you potentially open up your site to data mining and or session hijacking!

Initial Title
Drupal function to return contents of a databasetable (for Debugging)

Initial Tags
database, debug, drupal

Initial Language
PHP