wpdb::get_table_charset( string $table ): string|WP_Error
- Since
- 4.2.0
- Source
wp-includes/class-wpdb.php:3197
Compatibility
- WordPress
- since 4.2.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$tablestring- Table name.
Return value
string|WP_Error- Table character set, WP_Error object if it couldn't be found.
Performance profile
How much work a call to wpdb::get_table_charset() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Heavy
- Scaling
- Scales with input
- Instructions
- 14–74
- Plugin surface
- 1 hook
- Called by
- 4
Reaches the database via ->get_results().
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 133.
Third-party callbacks on 'pre_get_table_charset' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly - sqldatabase query
->get_results()called directly
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wpdb::get_table_charset() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 14–19 | strtolower(), apply_filters() |
$charset === null && !isset($tablekey) | 41 | strtolower(), apply_filters(), explode(), ->get_results(), __() |
$charset === null && !isset($tablekey) && $count !== 1 | 53–68 | strtolower(), apply_filters(), explode(), ->get_results() |
$charset === null && !isset($tablekey) && $count === 1 | 54–65 | strtolower(), apply_filters(), explode(), ->get_results(), key() |
$charset === null && !isset($tablekey) && !$columns && empty($column) | 60–61 | strtolower(), apply_filters(), explode(), ->get_results(), explode(), strtoupper() |
$charset === null && !isset($tablekey) && !$columns && !empty($column) | 73–74 | strtolower(), apply_filters(), explode(), ->get_results(), explode(), strtolower(), explode(), strtoupper() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 133 | 14–74 | 16 | |
| 8.5 | 133 | 14–74 | 16 | |
| 8.4 | 133 | 14–74 | 16 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 136 | 14–77 | 16 | |
| 8.2 | 136 | 14–77 | 16 | |
| 8.1 | 136 | 14–77 | 16 | |
| 7.4 | 136 | 14–77 | 16 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Hooks and filters fired · 1
One hook fires while wpdb::get_table_charset() runs, in this order:
- apply_filters( pre_get_table_charset )filterline 3212 (+15 into the body)
Filters the table charset value before the DB is checked.
Uses · 4
- apply_filters()Calls the callback functions that have been added to a filter hook.
- __()Retrieves the translation of $text.
- wpdb::get_results()Retrieves an entire SQL result set from the database (i.e., many rows).
- WP_Error::__construct()Initializes the error.
Used by · 4
- wpdb::check_safe_collation()Checks if the query is accessing a collation considered safe on the current version of MySQL.
- wpdb::get_col_charset()Retrieves the character set for the given column.
- wpdb::get_col_length()Retrieves the maximum string length allowed in a given column.
- wpdb::strip_invalid_text_from_query()Strips any invalid characters from the query.
Source code
protected function get_table_charset( $table ) { $tablekey = strtolower( $table ); /** * Filters the table charset value before the DB is checked. * * Returning a non-null value from the filter will effectively short-circuit * checking the DB for the charset, returning that value instead. * * @since 4.2.0 * * @param string|WP_Error|null $charset The character set to use, WP_Error object * if it couldn't be found. Default null. * @param string $table The name of the table being checked. */ $charset = apply_filters( 'pre_get_table_charset', null, $table ); if ( null !== $charset ) { return $charset; } if ( isset( $this->table_charset[ $tablekey ] ) ) { return $this->table_charset[ $tablekey ]; } $charsets = array(); $columns = array(); $table_parts = explode( '.', $table ); $table = '`' . implode( '`.`', $table_parts ) . '`'; $results = $this->get_results( "SHOW FULL COLUMNS FROM $table" ); if ( ! $results ) { return new WP_Error( 'wpdb_get_table_charset_failure', __( 'Could not retrieve table charset.' ) ); } foreach ( $results as $column ) { $columns[ strtolower( $column->Field ) ] = $column; } $this->col_meta[ $tablekey ] = $columns; foreach ( $columns as $column ) { if ( ! empty( $column->Collation ) ) { list( $charset ) = explode( '_', $column->Collation ); $charsets[ strtolower( $charset ) ] = true; } list( $type ) = explode( '(', $column->Type ); // A binary/blob means the whole query gets treated like this. if ( in_array( strtoupper( $type ), array( 'BINARY', 'VARBINARY', 'TINYBLOB', 'MEDIUMBLOB', 'BLOB', 'LONGBLOB' ), true ) ) { $this->table_charset[ $tablekey ] = 'binary'; return 'binary'; } } // utf8mb3 is an alias for utf8. if ( isset( $charsets['utf8mb3'] ) ) { $charsets['utf8'] = true; unset( $charsets['utf8mb3'] ); } // Check if we have more than one charset in play. $count = count( $charsets ); if ( 1 === $count ) { $charset = key( $charsets ); } elseif ( 0 === $count ) { // No charsets, assume this table can store whatever. $charset = false; } else { // More than one charset. Remove latin1 if present and recalculate. unset( $charsets['latin1'] ); $count = count( $charsets ); if ( 1 === $count ) { // Only one charset (besides latin1). $charset = key( $charsets ); } elseif ( 2 === $count && isset( $charsets['utf8'], $charsets['utf8mb4'] ) ) { // Two charsets, but they're utf8 and utf8mb4, use utf8. $charset = 'utf8'; } else {Changelog
Introduced in 4.2.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.7.7 tag, from
src/wp-includes/class-wpdb.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.