14

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
  • 314,254
  • 77
  • 507
  • 490
Run
  • 51,293
  • 159
  • 419
  • 719
  • 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,372
  • 15
  • 66
  • 132
johannes
  • 15,487
  • 3
  • 41
  • 57
15

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
  • 26,923
  • 21
  • 73
  • 125
Lorenz Lo Sauer
  • 22,244
  • 13
  • 79
  • 86
10

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
  • 26,923
  • 21
  • 73
  • 125
cubic1271
  • 1,068
  • 8
  • 14
0
$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
  • 26,923
  • 21
  • 73
  • 125
Clark Superman
  • 353
  • 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
  • 27,619
  • 11
  • 71
  • 89
Noor Khan
  • 540
  • 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