我只需要知道如何打印出表名和列,我尝试的所有方法都不起作用,尽管这显然非常简单。我到处都找过了,但找不到任何可以打印列的东西。有人可以帮忙吗?
最佳答案
以下代码应该提取您想要的信息。
<?php
$mysqli = new mysqli("hostname", "username", "password", "database");
if($mysqli->connect_errno)
{
echo "Error connecting to database.";
}
// Gather all table names into an array.
$query = "SHOW TABLES";
$result = $mysqli->query($query);
$tables = $result->fetch_all();
// Step through the array, only accessing the first element (the table name)
// and gather the column names in each table.
foreach($tables as $table)
{
echo "<h2>" . $table[0] . "</h2>";
$query = "DESCRIBE " . $table[0];
$result = $mysqli->query($query);
$columns = $result->fetch_all();
foreach($columns as $column)
{
echo $column[0] . "<br />";
}
}
$mysqli->close();
?>
关于php - 如何使用 mysqli 在 php 中打印数据库中表的名称以及这些表中的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37221762/