使用php查看mysql的所有库表结构

2022-03-29 15:00:45 浏览数 (1)

使用php查看mysql的所有库表结构

代码语言:javascript复制
主要思路:
1.查看所有的数据库;
2.选择某个数据库;
3.查看某个库下面的所有表;
4.查看表结构

主要sql语句:
show databases;
show tables;
show CREATE TABLE "$table"
代码语言:javascript复制
<?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: %sn", $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();

?>

本文为joshua317原创文章,转载请注明:转载自joshua317博客 https://www.joshua317.com/article/246

0 人点赞