1. 程式人生 > 其它 >使用php檢視mysql的所有庫表結構

使用php檢視mysql的所有庫表結構

使用php檢視mysql的所有庫表結構

主要思路:
1.檢視所有的資料庫;
2.選擇某個資料庫;
3.檢視某個庫下面的所有表;
4.查看錶結構

主要sql語句:
show databases;
show tables;
show CREATE TABLE "$table"
<?php
header('Content-Type: text/html; charset=utf-8');
$host = "xxx";
$user = "xxxx";
$passwd = "xxxx";

$mysqli = new mysqli($host, $user, $passwd);
$mysqli->set_charset("utf8_general_ci");


/* check connection */
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

$databases = $mysqli->query("show databases");
foreach ($databases as $v1) {
    $database = $v1['Database'];
    //排除掉不想檢視的庫
    if (in_array($database,['information_schema'])) {
        continue;
    }
    $mysqli->select_db($v1["Database"]);
    $mysqli->query("set character set 'utf8'");
    $tables = $mysqli->query("show tables");
    foreach ($tables as $v2) {
        $tableVal = array_values($v2);
        $table = $tableVal[0];
        $decs = $mysqli->query("show CREATE TABLE $table");
        foreach ($decs as $v3) {
            echo "<h3>資料表:" . $v1["Database"] . "." . $v3["Table"]."</h3><br/>";
            echo ($v3['Create Table']);
            echo "<br/>";
        }
    }

}
$mysqli->close();

?>