table_exists()
public function table_exists(string $table): bool
Description
Checks whether a specified table exists in the current database. This method uses MySQL's SHOW TABLES LIKE statement to verify table existence. Returns true if the table exists, false otherwise. Useful for conditional operations and preventing errors when working with dynamic table names.
Parameters
| Parameter | Type | Description | Default | Required |
|---|---|---|---|---|
| table | string | The name of the table to check for existence. | - | Yes |
Return Value
| Type | Description |
|---|---|
| bool | True if the specified table exists in the database, false if it doesn't exist or if an error occurs. |
Example #1: Basic Table Existence Check
The code sample below demonstrates how to check if a table exists before performing operations on it.
if ($this->db->table_exists('users')) {
echo "The 'users' table exists in the database.";
} else {
echo "The 'users' table does not exist.";
}Example #2: Conditional Table Creation/Usage
The code sample below demonstrates how to use table_exists() for conditional logic in application setup or migrations.
$table_name = 'user_sessions';
if (!$this->db->table_exists($table_name)) {
// Create the table if it doesn't exist
$sql = "CREATE TABLE $table_name (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT,
session_token VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)";
$this->db->query($sql);
echo "Table '$table_name' created successfully.";
} else {
echo "Table '$table_name' already exists.";
}Example #3: Dynamic Table Operations with Safety Check
The code sample below demonstrates how to safely perform operations on dynamically determined tables.
// Get table name from configuration or user input (sanitized)
$target_table = 'logs_' . date('Y_m');
if ($this->db->table_exists($target_table)) {
// Perform operations on the monthly log table
$log_count = $this->db->count($target_table);
echo "There are " . $log_count . " records in the " . $target_table . " table.";
// Insert new log entry
$log_data = [
'message' => 'System check completed',
'level' => 'INFO',
'timestamp' => date('Y-m-d H:i:s')
];
$this->db->insert($log_data, $target_table);
} else {
echo "Table '$target_table' not found. Monthly log table may need to be created.";
}Example #4: Checking Multiple Tables
The code sample below demonstrates how to verify the existence of multiple required tables.
$required_tables = ['users', 'products', 'orders', 'categories'];
$missing_tables = [];
foreach ($required_tables as $table) {
if (!$this->db->table_exists($table)) {
$missing_tables[] = $table;
}
}
if (empty($missing_tables)) {
echo "All required tables exist.";
} else {
echo "Missing tables: " . implode(', ', $missing_tables);
}Important Notes
- The
$tableparameter is required. - Uses MySQL's
SHOW TABLES LIKEstatement with parameter binding for security. - Returns
falseif the table doesn't exist OR if a database error occurs. - Case sensitivity depends on the underlying operating system and MySQL configuration.
- Only checks tables in the current database (specified during Db class instantiation).
- Useful for:
- Installation scripts and migrations
- Dynamic table operations
- Feature toggling based on table availability
- Preventing errors before table-specific operations
- For better performance in loops checking multiple tables, consider using
get_tables()once and checking the array. - This method is called internally by
validate_table_exists()before most database operations.