获取mysql数据库模式

问题描述 投票:0回答:2

是否可以以 json 或 xml 格式获取 mySQL 数据库的完整结构?

我正在寻找的是一种获取架构(存储的数据无关紧要)的方法,以使用它为应用程序创建后端/crud。

php mysql
2个回答
2
投票

这不是问题的完全全面的答案,因为它不返回 json。但它是一个简单的类,以对象形式提供有关表的所有相关信息。这可以很容易地转换为返回 json。

该类依赖于传递一个实现

rawQuery
方法的 db 对象,如 PHP-Mysqli-Database-Class 所做的那样。但如果需要,您可以轻松重写该方法
extractTableInfo

<?php
Class dbHelper {
    private $db;

    public function __construct($db) {
        $this->db = $db;
    }

    public function extractTableInfo($tableName) {
        $result = $this->db->rawQuery("DESCRIBE `".$tableName."`");

        $tableInfo = array();
        foreach ($result as $key => $value) {
            $info = new \stdClass();
            $info->name = $value['Field'];
            $info->type = $this->_getColumnType($value['Type']);
            $info->length = $this->_getLength($value['Type']);
            $info->hasNull = $this->_getNull($value['Null']);
            $info->default = $this->_getDefault($value['Default']);
            $tableInfo[] = $info;
        }
        return $tableInfo;
    }

    public function getIndexes($tableName) {
        $result = $this->db->rawQuery("SHOW INDEX FROM `".$tableName."`");
        return $result;
    }



    private function _getLength($info) {
        $pattern = '/\({1}([\d\W]*)\){1}/';
        preg_match($pattern, $info, $matches);
        return isset($matches[1]) ? $matches[1] : NULL;
    }

    private function _getColumnType($info) {
        $pattern = '/([\w]*)(\([\d\W]*\))*/';
        preg_match($pattern, $info, $matches);
        return isset($matches[1]) ? $matches[1] : NULL;
    }

    private function _getNull($info) {
        if($info==='NO') {
            return false;
        } else {
            return true;
        }
    }

    private function _getDefault($info) {
        if($info>='') {
            return $info;
        } else {
            return NULL;
        }
    }

}

用途:

$db = YOUR DATABASE OBJECT/CLASS;
$dbHelper = new dbHelper($db);
$tableName = "test";
$tableInfo = $dbHelper->extractTableInfo($tableName);
echo json_encode($tableInfo);

要获取数据库中所有表的列表,您需要:

SHOW TABLES [in dbname]

或关注这个问题

仍然有许多具有不同好处/产出的替代方案:

  • 使用mysql工作台
  • 使用 phpmyadmin
  • 使用命令行
    mysqldump -u root -p --no-data dbname > schema.sql

0
投票

我的答案可能也不适合100%你的问题,因为它也不会返回json。但它可能对某人有帮助

function makeTable($query, $array = null)
{
    $stmt = makeStatement($query, $array);

    if ($stmt instanceof Exception) {
        echo $stmt->getCode() . ': ' . $stmt->getMessage();
    } else {
        $meta = array();
        echo '<table class ="table"
     <tr>';

        for ($i = 0; $i < $stmt->columnCount(); $i++) {
            $meta[] = $stmt->getColumnMeta($i);
            echo '<th>' . $meta[$i]['name'] . '</th>';
        }
        echo '<th></th>';
        echo '</tr>';


        while ($databases = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo '<tr class="clickable-row">';
            foreach ($databases as $database) {
                echo '<td>' . $database . '</td>';
            }
            echo '<form method="post" action="index.php?site=tables">';
            echo '<input type="hidden" name="database" value="' . $database . '">';
            echo '<td>';
            echo '<button type="submit" class="btn btn-outline-secondary" name="choose">Auswählen</button>';
            echo '</td>';
            echo '</form>';
            echo '</tr>';
        }
        echo '</table>';
    }
}

这是tables.php:

<?php
$schema;

if (isset($_POST['database'])) {
    $schema = $_POST['database'];
    echo "Schema: " . $schema . "<br>";
}

$query = "show tables from " . $schema;
$stmt = makeStatement($query);

if ($stmt instanceof Exception) {
    echo $stmt->getCode() . ': ' . $stmt->getMessage();
} else {
    $meta = array();
    echo '<table class ="table"
     <tr>';

    for ($i = 0; $i < $stmt->columnCount(); $i++) {
        $meta[] = $stmt->getColumnMeta($i);
        echo '<th>' . $meta[$i]['name'] . '</th>';
    }
    echo '<th></th>';
    echo '<th></th>';
    echo '</tr>';


    while ($tables = $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo '<tr class="clickable-row">';
        foreach ($tables as $table) {
            echo '<td>' . $table . '</td>';
        }
        echo '<form method="post" action="index.php?site=columnsInhalt">';
        echo '<input type="hidden" name="schema" value="' . $schema . '">';
        echo '<input type="hidden" name="table" value="' . $table . '">';
        echo '<td>';
        echo '<button type="submit" class="btn btn-outline-secondary" name="inhalt">Inhalt</button>';
        echo '</td>';
        echo '</form>';

        echo '<form method="post" action="index.php?site=columnsStructure">';
        echo '<input type="hidden" name="schema" value="' . $schema . '">';
        echo '<input type="hidden" name="table" value="' . $table . '">';
        echo '<td>';
        echo '<button type="submit" class="btn btn-outline-secondary" name="structur">Struktur</button>';
        echo '</td>';
        echo '</form>';
        echo '</tr>';
    }
    echo '</table>';
}

这里是 Inhalt 列和Structure 列

<?php
$schema;

if (isset($_POST['schema']) && isset($_POST['table'])) {
    $schema = $_POST['schema'];
    $table = $_POST['table'];
    echo 'Schema: ' . $schema . "<br>";
    echo 'Table: ' . $table . "<br>";
}

$query = "select * from " . $schema . "." . $table;
$query = "show columns from " . $schema . "." . $table; //for structure
$stmt = makeStatement($query);

if ($stmt instanceof Exception) {
    echo $stmt->getCode() . ': ' . $stmt->getMessage();
} else {
    $meta = array();
    echo '<table class ="table"
        <tr>';
            for ($i = 0; $i < $stmt->columnCount(); $i++) {
                $meta[] = $stmt->getColumnMeta($i);
                echo '<th>' . $meta[$i]['name'] . '</th>';
            }
        echo '</tr>';

        while ($columns = $stmt->fetch(PDO::FETCH_ASSOC)) {
            echo '<tr class="clickable-row">';
                foreach ($columns as $column) {
                    echo '<td>' . $column . '</td>';
                }
            echo '</tr>';
        }
    echo '</table>';
}
© www.soinside.com 2019 - 2024. All rights reserved.