Posted By

berkes on 08/01/08


Tagged

database debug drupal helper tool


Versions (?)


Advertising

Website Promotion DIRECTORY is a crucial factor for all websites that need to gain better organic search engine rankings and increase website traffic.
Submitting your website as part of your Web Promotion strategy to our SEO friendly and high traffic Business Directory for review is an excellent way to gain a valuable backlink and increase your websites visibility online.

Submit Site


Who likes this?

1 person has marked this snippet as a favorite

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!

Expand | Embed | Plain Text
  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.

Download royalty free graphics