php 使用 MySQLi 列出数据库中的所有表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4703111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
list all tables in a database with MySQLi
提问by laukok
I have looked around and still can't find how to list all my tables in a database. is it possible with MySQLi?
我环顾四周,仍然无法找到如何在数据库中列出我的所有表。MySQLi 有可能吗?
Thanks.
谢谢。
回答by johannes
There are many ways.
有很多方法。
SHOW TABLES
Is the most simple SQL statement for doing that. You can also take a look at INFORMATION_SCHEMA.TABLES
if you want to have more details or do some filtering or such.
是最简单的 SQL 语句。您还可以查看INFORMATION_SCHEMA.TABLES
是否想要了解更多详细信息或进行一些过滤等。
SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE 'your_database';
回答by Lorenz Lo Sauer
Using PHP 5.5 or later, a simple solution is using PHP's built-in array_column()function.
使用 PHP 5.5 或更高版本,一个简单的解决方案是使用 PHP 的内置array_column()函数。
$link = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
$listdbtables = array_column(mysqli_fetch_all($link->query('SHOW TABLES')),0);
回答by cubic1271
I'd try something like:
我会尝试类似的东西:
function get_tables()
{
$tableList = array();
$res = mysqli_query($this->conn,"SHOW TABLES");
while($cRow = mysqli_fetch_array($res))
{
$tableList[] = $cRow[0];
}
return $tableList;
}
You might also be interested in skimming this: https://devzone.zend.com/13/php-101-part-8-databases-and-other-animals_part-2/(EDIT: this link refers to mysql API and not mysqli, but most calls do have a mysqli parallel).
您可能也有兴趣略读:https: //devzone.zend.com/13/php-101-part-8-databases-and-other-animals_part-2/(编辑:此链接指的是 mysql API 而不是mysqli,但大多数调用确实有一个 mysqli 并行)。
HTH
HTH
回答by Noor Khan
here is little example
这是小例子
class database {
public $connection;
function __construct() {
$this->connection = mysqli_connect(DBHOST,
DBUSER,
DBPASS,
DBNAME) or
die('Database Connection Error: '.mysqli_connect_error());
}
public function close_database() {
return mysqli_close($this->connection);
}
public function query($query) {
$query = mysqli_query($this->connection ,$query) or die($this->show_errors('Query Execution Error: '.mysqli_error($this->connection),'E'));
return $query;
}
public function fetch_assoc($query) {
$query = mysqli_fetch_assoc($query);
return $query;
}
}
$db = new database();
$query = $db->query("SHOW TABLES FROM DATABASENAME");
$db->fetch_assoc($query);