list all tables in a database with MySQLi

66,504

Solution 1

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.

SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA LIKE 'your_database';

Solution 2

Using PHP 5.5 or later, a simple solution is using PHP's built-in array_column() function.

$link = mysqli_connect(DBHOST, DBUSER, DBPASS, DBNAME);
$listdbtables = array_column($link->query('SHOW TABLES')->fetch_all(),0);

Solution 3

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;
}
Share:
66,504
Run
Author by

Run

A cross-disciplinary full-stack web developer/designer.

Updated on June 10, 2021

Comments

  • Run
    Run almost 3 years

    I have looked around and still can't find how to list all my tables in a database. is it possible with MySQLi?

    Thanks.