18

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

Thanks.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
Run
  • 54,938
  • 169
  • 450
  • 748
  • 3
    http://dev.mysql.com/doc/refman/5.0/en/show-tables.html – Pekka Jan 16 '11 at 00:51
  • Does this answer your question? [How to display all table names from particular mysql database in php](https://stackoverflow.com/questions/45467290/how-to-display-all-table-names-from-particular-mysql-database-in-php) – Nico Haase Jun 10 '21 at 09:36

5 Answers5

18

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';
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
johannes
  • 15,807
  • 3
  • 44
  • 57
17

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);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Lorenz Lo Sauer
  • 23,698
  • 16
  • 85
  • 87
11

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;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
cubic1271
  • 1,118
  • 8
  • 14
1
$link = mysqli_connect("localhost", "domain_root", "##pwd", "domain_DBname");

$query = mysqli_query($link, "SHOW TABLES IN domain_DBname");
$numrows = mysqli_num_rows($query);
echo "<b>Amount of tables: ".$numrows." and their names:</b>";
while ($row = mysqli_fetch_array($query)) {
    echo $row[0]." ";
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Clark Superman
  • 373
  • 2
  • 13
-1

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);
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
Noor Khan
  • 528
  • 1
  • 5
  • 12
  • 1
    Instead of creating this class as a wrapper just to use procedural functions, you should just use the MySQLi class. – Nathan F. Sep 11 '16 at 20:10