get_tables()

public function get_tables(): array

Description

Retrieves an array of all table names in the current database. This method executes MySQL's SHOW TABLES command and returns the results as a simple indexed array. Useful for database introspection, installation scripts, and administrative tools.

Parameters

Parameter Type Description Default Required
none none This method takes no parameters. - -

Return Value

Type Description
array An indexed array of table names (strings) on success, or an empty array on failure.

Example #1: Listing All Database Tables

The code sample below demonstrates how to retrieve and display all tables in the database.

PHP
$tables = $this->db->get_tables();

if (!empty($tables)) {
    echo "Tables in the database:";
    echo "<ul>";
    foreach ($tables as $table) {
        echo "<li>" . $table . "</li>";
    }
    echo "</ul>";
    
    echo "Total tables: " . count($tables);
} else {
    echo "No tables found or unable to retrieve table list.";
}

Example #2: Checking for Required Tables

The code sample below demonstrates how to verify that specific tables exist in the database.

PHP
$required_tables = ['users', 'products', 'orders', 'categories'];
$available_tables = $this->db->get_tables();
$missing_tables = [];

foreach ($required_tables as $required) {
    if (!in_array($required, $available_tables)) {
        $missing_tables[] = $required;
    }
}

if (empty($missing_tables)) {
    echo "All required tables are present.";
} else {
    echo "Missing tables: " . implode(', ', $missing_tables);
    echo "Available tables: " . implode(', ', $available_tables);
}

Example #3: Performing Operations on All Tables

The code sample below demonstrates how to iterate through all tables and perform operations like counting records.

PHP
$tables = $this->db->get_tables();
$table_stats = [];

if (!empty($tables)) {
    foreach ($tables as $table) {
        try {
            $count = $this->db->count($table);
            $table_stats[$table] = $count;
        } catch (Exception $e) {
            $table_stats[$table] = 'Error: ' . $e->getMessage();
        }
    }
    
    echo "Table Statistics:";
    echo "<table border='1'>";
    echo "<tr><th>Table</th><th>Record Count</th></tr>";
    foreach ($table_stats as $table => $count) {
        echo "<tr><td>" . $table . "</td><td>" . $count . "</td></tr>";
    }
    echo "</table>";
}

Example #4: Filtering Tables by Pattern

The code sample below demonstrates how to filter tables by name pattern (e.g., all log tables).

PHP
$all_tables = $this->db->get_tables();
$log_tables = [];

foreach ($all_tables as $table) {
    if (strpos($table, 'log_') === 0 || strpos($table, '_logs') !== false) {
        $log_tables[] = $table;
    }
}

if (!empty($log_tables)) {
    echo "Log tables found: " . implode(', ', $log_tables);
    
    // Archive old log tables
    foreach ($log_tables as $log_table) {
        if (strpos($log_table, '2023_') !== false) { // 2023 logs
            // Archive logic here
            echo "Archiving: " . $log_table;
        }
    }
} else {
    echo "No log tables found.";
}

Important Notes

  • This method takes no parameters.
  • Returns an empty array ([]) if no tables exist or if a database error occurs.
  • Uses MySQL's SHOW TABLES command which is MySQL-specific.
  • Returns table names as strings in an indexed array (not associative).
  • Only returns tables from the current database (the one connected to via the Db class).
  • Does not include views, only actual tables.
  • Table names are returned without database prefix (just the table names).
  • Case sensitivity of table names depends on the underlying operating system and MySQL configuration.
  • Useful for:
    1. Database administration tools
    2. Installation/migration scripts
    3. Database introspection and reporting
    4. Dynamic table operations
    5. Backup systems
  • For better performance when checking multiple individual tables, use this method once rather than calling table_exists() multiple times.
  • This method is used internally by validate_table_exists() to provide detailed error messages in development mode.