<?php
//  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// * * * * * * * * * * * * * * * * *
// db config
$dblocation = "XXXXXXXX";
$dbname = "XXXXXXXX";
$dbuser = "XXXXXXXX";
$dbpwd = "XXXXXXXX";

// mysql connection
$connection = mysql_connect( $dblocation, $dbuser, $dbpwd);
if (!$connection) {
die(' Connection failed: ' . mysql_error());
}
//  database
$db_selected = mysql_select_db($dbname, $connection);
if (!$db_selected) {
    die (' Can\'t select database: ' . mysql_error());
}
// * * * * * * * * * * * * * * * * *
?><html>
<head>
<title><?php echo "[$dbname]" ?> structure</title>
<meta name="robots" content="none" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" /> 

<style type="text/css">
 html, body {font-size:small;font-family:courier;color:#444}
 h1 {font-size:110%;text-align:center; color:#bbc5ce;padding:8px 0; font-weight:normal} 
 ol {padding: 0px 10px 12px 10px;font-size: 90%;border-bottom:1px dotted #aaa }
 table{ margin: 10px auto; border:0;font-size: 90%;width: 99%}
 td {padding-left:5px}
 #footer {margin-top:12px;border-top:1px dotted #aaa;padding:5px;text-align:center}
.count {font-size: 90%;text-align:center; padding-top:10px;border-top:1px dotted #aaa}
 .print {text-align:center}
 a {color:#f55;font-size:85%; font-family:verdana}
 
  @media only screen and (min-width: 550px) {
 table {width: 90%}
 ol {
 -webkit-column-count: 2; /* Chrome, Safari, Opera */
 -moz-column-count: 2; /* Firefox */
 column-count: 2;
 }
 }
 
  @media only screen and (min-width: 800px) {
 table {width: 80%}
 ol {
 -webkit-column-count: 4; /* Chrome, Safari, Opera */
 -moz-column-count: 4; /* Firefox */
 column-count: 4;
 }
 }
 
 @media print {
  * {text-align:left !important; color:#000 !important; font-size:11px !important;margin:0 !important;  border: none !important;}
   table {display:none}
  .print  {display:none}
  .count {padding-bottom:10px}
 }
</style>

</head>
<body>
<?php


/* LIST  */

$result = mysql_list_tables($dbname);

if (!$result) {
    print "DB Error, could not list tables\n";
    print 'MySQL Error: '.mysql_error();
    exit;
}

if ($_GET['lime_survey_'] == null) {

    print "<h1>STRUCTURE OF '$dbname' TABLE</h1>  \n";
    $tbl_List = mysql_list_tables($dbname);
    $i = 0;
    while ($tables = mysql_tablename($tbl_List, $i)) {
        $i++;
    }
    echo "<p class=\"count\">TOTAL COUNT : $i tables</p>";

    print "<ol id=\"list\"> \n";
    while ($row = mysql_fetch_row($result)) {
        print " <li>$row[0]</li> \n";
    }
    print "</ol>  \n<p class=\"print\"><a href=\"javascript:window.print();\">Print database structure</a></p> \n";
} else {

}
mysql_free_result($result);

/*
DETAILS 
https://jadendreamer.wordpress.com/2009/01/13/print-all-mysql-database-tables-fields-using-php/
*/

// loop to show all the tables and fields
$loop = mysql_query("SHOW tables FROM $dbname")
or die ('cannot select tables');

while($table = mysql_fetch_array($loop))
{

    echo "
        <table cellpadding=\"2\" cellspacing=\"2\" >
            <tr bgcolor=\"#bbc5ce\">
                <td colspan=\"5\" align=\"center\"><b><font color=\"#FFFFFF\">" . $table[0] . "</font></td>
            </tr>
            <tr style=\"color:#bbc5ce;text-align:center\">
                <td>Field</td>
                <td>Type</td>
                <td>Key</td>
                <td>Default</td>
                <td>Extra</td>
            </tr>";

    $i = 0; //row counter
    $row = mysql_query("SHOW columns FROM " . $table[0])
    or die ('cannot select table fields');

    while ($col = mysql_fetch_array($row))
    {
        echo "<tr";

        if ($i % 2 == 0)
            echo " bgcolor=\"#e9efef\"";

        echo ">
            <td>" . $col[0] . "</td>
            <td>" . $col[1] . "</td>
            <td>" . $col[2] . "</td>
            <td>" . $col[3] . "</td>
            <td>" . $col[4] . "</td>
        </tr>";

        $i++;
    } //end row loop

    echo "</table> \n\n";
} //end table loop

?>
<div id ="footer"><?php echo date(DATE_RFC2822); 

// * * * * * * * * * * * * * * * * *
mysql_close($connection);
?></div>
</body>
</html>