Posted By

berkes on 08/01/08


Tagged

database debug drupal helper tool


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

comfixit
basicmagic


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


 / Published in: PHP
 

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!

  1. function helpers_database_render_table($table_name) {
  2. $columns = $header = $rows = array();
  3.  
  4. $result = db_queryd('SHOW columns in {%s}', $table_name);
  5. while ($column = db_fetch_object($result)) {
  6. $header[] = t('@columnname (@columntype)', array('@columnname' => $column->Field, '@columntype' => $column->Type));
  7. $columns[] = $column->Field;
  8. }
  9.  
  10. $result = pager_query('SELECT * FROM {%s}', 50, 0, NULL, $table_name);
  11. while ($record = db_fetch_object($result)) {
  12. foreach($columns as $column) {
  13. //@TODO: include spans with title=fullresult on large results, and add ellipses in that case.
  14. $row[$column] = drupal_substr($record->$column, 0, 36);
  15. }
  16. $rows[] = $row;
  17. }
  18.  
  19. $count = db_result(db_query("SELECT count(%s) FROM {%s}", $columns[0], $table_name));
  20.  
  21. $caption = t('SELECT * FROM {%table_name} resulted in %count results', array('%table_name' => $table_name, '%count' => $count));
  22.  
  23. return theme('table', $header, $rows, array(), $caption) . theme('pager');
  24. }

Report this snippet  

You need to login to post a comment.