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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:03:30  来源:igfitidea点击:

list all tables in a database with MySQLi

phpmysqlsqlmysqli

提问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.TABLESif 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);