Listing MySQL Tables with PHP

Listing table names does not require the removed mysql_list_tables() function. MySQL provides SHOW TABLES, and PHP can execute that statement through PDO or MySQLi.

SHOW TABLES

SHOW TABLES;

PDO example

<?php
$stmt = $dbo->query('SHOW TABLES');
while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
    echo htmlspecialchars($row[0], ENT_QUOTES, 'UTF-8') . '<br>';
}
?>

Filter table names with LIKE

SHOW TABLES LIKE 't%';
<?php
$stmt = $dbo->query("SHOW TABLES LIKE 't%'");
foreach ($stmt->fetchAll(PDO::FETCH_COLUMN) as $table) {
    echo htmlspecialchars($table, ENT_QUOTES, 'UTF-8') . '<br>';
}
?>

MySQLi example

<?php
$result = $connection->query('SHOW TABLES');
echo 'Number of tables: ' . $result->num_rows . '<br>';
while ($row = $result->fetch_row()) {
    echo htmlspecialchars($row[0], ENT_QUOTES, 'UTF-8') . '<br>';
}
?>

Historical mysql_list_tables()

<?php
// Historical only: removed from PHP 7+
$list = mysql_list_tables('sql_tutorial');
while ($i < mysql_num_rows($list)) {
    $tableName = mysql_tablename($list, $i);
    $i++;
}
?>

For portable metadata queries, you can also query INFORMATION_SCHEMA.TABLES. Related: SHOW TABLES tutorial, LIKE patterns, listing databases, and MySQLi.

← All PHP & MySQL topics




Subscribe to our YouTube Channel here



plus2net.com




SQL Video Tutorials










We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2026   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer