参考:https://qiita.com/arc279/items/0a3895a04fc814512baf
<?php
$dsn = "mysql:host=127.0.0.1;port=3306;charset=utf8";
$user = "root";
$pass = "";
$DBNAME = "employees";
$SQL_TABLE_INFO = <<<EOD
SELECT
TABLE_NAME,
TABLE_COMMENT
FROM INFORMATION_SCHEMA.TABLES
WHERE table_schema = '$DBNAME'
EOD;
$SQL_COLUMN_INFO = <<<EOD
SELECT
COLUMN_NAME,
COLUMN_DEFAULT,
IS_NULLABLE,
COLUMN_TYPE,
COLLATION_NAME,
COLUMN_KEY,
EXTRA,
COLUMN_COMMENT
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema = '$DBNAME'
AND table_name LIKE :tbl
EOD;
function each_tables($pdo, $dbname, $callback) {
global $SQL_TABLE_INFO;
global $SQL_COLUMN_INFO;
$stmt1 = $pdo->query($SQL_TABLE_INFO);
while(false != ($tblinfo = $stmt1->fetch(PDO::FETCH_ASSOC))) {
$tblname = $tblinfo["TABLE_NAME"];
$stmt2 = $pdo->prepare($SQL_COLUMN_INFO);
$ret2 = $stmt2->execute(array(":tbl" => $tblname));
if($ret2 === false) {
throw new Exception($tblname);
}
$colinfo = $stmt2->fetchAll(PDO::FETCH_ASSOC);
$callback($tblinfo, $colinfo);
}
}
function genHtml($pdo, $dbname) {
$str = <<<'EOD'
?>
<meta charset="utf-8">
<?php
each_tables($pdo, $dbname, function($tblinfo, $colinfo) {
?>
<fieldset>
<legend><?= $tblinfo["TABLE_NAME"] ?></legend>
<p><?= $tblinfo["TABLE_COMMENT"] ?></p>
<table border="1">
<tr>
<?php foreach(array_keys($colinfo[0]) as $key): ?>
<th><?= $key ?></th>
<?php endforeach; ?>
</tr>
<?php foreach($colinfo as $col): ?>
<tr>
<?php foreach($col as $key => $val): ?>
<td><?= $val ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
</fieldset>
<?php
});
?>
EOD;
eval($str);
}
try {
$pdo = new PDO($dsn, $user, $pass);
genHtml($pdo, $DBNAME);
} catch(Exception $e) {
var_dump($e);
throw $e;
}