Title: wpdb::get_col_length
Published: April 27, 2015
Last modified: May 20, 2026

---

# wpdb::get_col_length( string $table, string $column ): array|false|󠀁[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)󠁿

## In this article

 * [Description](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#wp--skip-link--target)

Retrieves the maximum string length allowed in a given column.

## 󠀁[Description](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#description)󠁿

The length may either be specified as a byte length or a character length.

## 󠀁[Parameters](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#parameters)󠁿

 `$table`stringrequired

Table name.

`$column`stringrequired

Column name.

## 󠀁[Return](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#return)󠁿

 array|false|[WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
Array of column length information, false if the column has no length (for example,
numeric column), [WP_Error](https://developer.wordpress.org/reference/classes/wp_error/)
object if there was an error.

 * `type` string
 * One of `'byte'` or `'char'`.
 * `length` int
 * The column length.

## 󠀁[Source](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#source)󠁿

    ```php
    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;
    	}
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wpdb.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.0/src/wp-includes/class-wpdb.php#L3374)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/class-wpdb.php#L3374-L3450)

## 󠀁[Related](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#related)󠁿

| Uses | Description | 
| [wpdb::get_table_charset()](https://developer.wordpress.org/reference/classes/wpdb/get_table_charset/)`wp-includes/class-wpdb.php` |

Retrieves the character set for the given table.

  | 
| [is_wp_error()](https://developer.wordpress.org/reference/functions/is_wp_error/)`wp-includes/load.php` |

Checks whether the given variable is a WordPress Error.

  |

| Used by | Description | 
| [wp_get_comment_fields_max_lengths()](https://developer.wordpress.org/reference/functions/wp_get_comment_fields_max_lengths/)`wp-includes/comment.php` |

Retrieves the maximum character lengths for the comment form fields.

  | 
| [wpdb::process_field_lengths()](https://developer.wordpress.org/reference/classes/wpdb/process_field_lengths/)`wp-includes/class-wpdb.php` |

For string fields, records the maximum string length that field can safely save.

  | 
| [wpdb::strip_invalid_text_for_column()](https://developer.wordpress.org/reference/classes/wpdb/strip_invalid_text_for_column/)`wp-includes/class-wpdb.php` |

Strips any invalid characters from the string for a given table and column.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/classes/wpdb/get_col_length/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.2.1](https://developer.wordpress.org/reference/since/4.2.1/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fclasses%2Fwpdb%2Fget_col_length%2F)
before being able to contribute a note or feedback.