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.
Example #2: Checking for Required Tables
The code sample below demonstrates how to verify that specific tables exist in the database.
Example #3: Performing Operations on All Tables
The code sample below demonstrates how to iterate through all tables and perform operations like counting records.
Example #4: Filtering Tables by Pattern
The code sample below demonstrates how to filter tables by name pattern (e.g., all log tables).
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 TABLEScommand 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:
- Database administration tools
- Installation/migration scripts
- Database introspection and reporting
- Dynamic table operations
- 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.