Skip to content

Instantly share code, notes, and snippets.

@seanbuscay
Created May 23, 2013 14:32
Show Gist options
  • Save seanbuscay/f8bcf2059f7dc2751218 to your computer and use it in GitHub Desktop.
Save seanbuscay/f8bcf2059f7dc2751218 to your computer and use it in GitHub Desktop.
Output mysql table to html.
//The CSS
//table.db-table { border-right:1px solid #ccc; border-bottom:1px solid #ccc; }
//table.db-table th { background:#eee; padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc; }
//table.db-table td { padding:5px; border-left:1px solid #ccc; border-top:1px solid #ccc; }
//Some CSS properties may need to be vendor-prefixed.
//The CSS I'm styling the table with is as basic as it gets -- style as you wish!
//The PHP / MySQL
/* connect to the db */
$connection = mysql_connect('localhost','username','password');
mysql_select_db('my_db',$connection);
/* show tables */
$result = mysql_query('SHOW TABLES',$connection) or die('cannot show tables');
while($tableName = mysql_fetch_row($result)) {
$table = $tableName[0];
echo '<h3>',$table,'</h3>';
$result2 = mysql_query('SHOW COLUMNS FROM '.$table) or die('cannot show columns from '.$table);
if(mysql_num_rows($result2)) {
echo '<table cellpadding="0" cellspacing="0" class="db-table">';
echo '<tr><th>Field</th><th>Type</th><th>Null</th><th>Key</th><th>Default<th>Extra</th></tr>';
while($row2 = mysql_fetch_row($result2)) {
echo '<tr>';
foreach($row2 as $key=>$value) {
echo '<td>',$value,'</td>';
}
echo '</tr>';
}
echo '</table><br />';
}
}
//The first step in the process is accessing all of the tables within the database.  Once all tables have been fetched, the next step is to loops through the array of tables we receive and, for each table, build a new HTML table with column information.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment