wpdb::get_col_length( string $table, string $column ): array|false|WP_Error
- Since
- 4.2.1
- Source
wp-includes/class-wpdb.php:3370
Description
The length may either be specified as a byte length or a character length.
Compatibility
- WordPress
- since 4.2.1
- 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.
$columnstring- Column name.
Return value
array|false|WP_Error- Array of column length information, false if the column has no length (for example, numeric column), WP_Error object if there was an error.
$typestringOne of 'byte' or 'char'.$lengthintThe column length.
Performance profile
How much work a call to wpdb::get_col_length() 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
- Trivial
- Scaling
- Constant
- Instructions
- 13–72
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 89.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action()one call below wpdb::get_col_length()
What one call costs · 4 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wpdb::get_col_length() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 13–20 | strtolower(), strtolower() |
!empty($value) && empty($tablekey) | 24–28 | strtolower(), strtolower(), ->get_table_charset(), is_wp_error() |
!empty($value) && !empty($tablekey) && !empty($columnkey) | 38–64 | strtolower(), strtolower(), explode(), strtolower() |
!empty($value) && empty($tablekey) && !is_wp_error() && !empty($columnkey) | 46–72 | strtolower(), strtolower(), ->get_table_charset(), is_wp_error(), explode(), strtolower() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 89 | 13–72 | 18 | |
| 8.5 | 89 | 13–72 | 18 | |
| 8.4 | 89 | 13–72 | 18 | 4 fewer instructions than PHP 8.3 |
| 8.3 | 93 | 13–76 | 18 | |
| 8.2 | 93 | 13–76 | 18 | |
| 8.1 | 93 | 13–76 | 18 | 1 more instruction than PHP 7.4 |
| 7.4 | 92 | 13–79 | 18 |
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.
Uses · 2
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wpdb::get_table_charset()Retrieves the character set for the given table.
Used by · 2
- wpdb::process_field_lengths()For string fields, records the maximum string length that field can safely save.
- wpdb::strip_invalid_text_for_column()Strips any invalid characters from the string for a given table and column.
Source code
public function get_col_length( $table, $column ) { $tablekey = strtolower( $table ); $columnkey = strtolower( $column ); // Skip this entirely if this isn't a MySQL database. if ( empty( $this->is_mysql ) ) { return false; } if ( empty( $this->col_meta[ $tablekey ] ) ) { // This primes column information for us. $table_charset = $this->get_table_charset( $table ); if ( is_wp_error( $table_charset ) ) { return $table_charset; } } if ( empty( $this->col_meta[ $tablekey ][ $columnkey ] ) ) { return false; } $typeinfo = explode( '(', $this->col_meta[ $tablekey ][ $columnkey ]->Type ); $type = strtolower( $typeinfo[0] ); if ( ! empty( $typeinfo[1] ) ) { $length = trim( $typeinfo[1], ')' ); } else { $length = false; } switch ( $type ) { case 'char': case 'varchar': return array( 'type' => 'char', 'length' => (int) $length, ); case 'binary': case 'varbinary': return array( 'type' => 'byte', 'length' => (int) $length, ); case 'tinyblob': case 'tinytext': return array( 'type' => 'byte', 'length' => 255, // 2^8 - 1 ); case 'blob': case 'text': return array( 'type' => 'byte', 'length' => 65535, // 2^16 - 1 ); case 'mediumblob': case 'mediumtext': return array( 'type' => 'byte', 'length' => 16777215, // 2^24 - 1 ); case 'longblob': case 'longtext': return array( 'type' => 'byte', 'length' => 4294967295, // 2^32 - 1 ); default: return false; } }Changelog
Introduced in 4.2.1. 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.8.8 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.