Title: wpdb::get_table_from_query
Published: April 23, 2015
Last modified: August 20, 2026

---

# wpdb::get_table_from_query( string $query ): string|false

## In this article

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

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

Finds the first table name referenced in a query.

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

 `$query`stringrequired

The query to search.

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

 string|false The table name found, or false if a table couldn’t be found.

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

    ```php
    protected function get_table_from_query( $query ) {
    	// Remove characters that can legally trail the table name.
    	$query = rtrim( $query, ';/-#' );

    	// Allow (select...) union [...] style queries. Use the first query's table name.
    	$query = ltrim( $query, "\r\n\t (" );

    	// Strip everything between parentheses except nested selects.
    	$query = preg_replace( '/\((?!\s*select)[^(]*?\)/is', '()', $query );

    	// Strip any leading SET STATEMENT statements.
    	$query = preg_replace( '/^SET STATEMENT.+?\sFOR\s+/is', '', $query );

    	// Quickly match most common queries.
    	if ( preg_match(
    		'/^\s*(?:'
    			. 'SELECT.*?\s+FROM'
    			. '|INSERT(?:\s+LOW_PRIORITY|\s+DELAYED|\s+HIGH_PRIORITY)?(?:\s+IGNORE)?(?:\s+INTO)?'
    			. '|REPLACE(?:\s+LOW_PRIORITY|\s+DELAYED)?(?:\s+INTO)?'
    			. '|UPDATE(?:\s+LOW_PRIORITY)?(?:\s+IGNORE)?'
    			. '|DELETE(?:\s+LOW_PRIORITY|\s+QUICK|\s+IGNORE)*(?:.+?FROM)?'
    		. ')\s+((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)/is',
    		$query,
    		$maybe
    	) ) {
    		return str_replace( '`', '', $maybe[1] );
    	}

    	// SHOW TABLE STATUS and SHOW TABLES WHERE Name = 'wp_posts'
    	if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES).+WHERE\s+Name\s*=\s*("|\')((?:[0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)\\1/is', $query, $maybe ) ) {
    		return $maybe[2];
    	}

    	/*
    	 * SHOW TABLE STATUS LIKE and SHOW TABLES LIKE 'wp\_123\_%'
    	 * This quoted LIKE operand seldom holds a full table name.
    	 * It is usually a pattern for matching a prefix so we just
    	 * strip the trailing % and unescape the _ to get 'wp_123_'
    	 * which drop-ins can use for routing these SQL statements.
    	 */
    	if ( preg_match( '/^\s*SHOW\s+(?:TABLE\s+STATUS|(?:FULL\s+)?TABLES)\s+(?:WHERE\s+Name\s+)?LIKE\s*("|\')((?:[\\\\0-9a-zA-Z$_.-]|[\xC2-\xDF][\x80-\xBF])+)%?\\1/is', $query, $maybe ) ) {
    		return str_replace( '\\_', '_', $maybe[2] );
    	}

    	// Big pattern for the rest of the table-related queries.
    	if ( preg_match(
    		'/^\s*(?:'
    			. '(?:EXPLAIN\s+(?:EXTENDED\s+)?)?SELECT.*?\s+FROM'
    			. '|DESCRIBE|DESC|EXPLAIN|HANDLER'
    			. '|(?:LOCK|UNLOCK)\s+TABLE(?:S)?'
    			. '|(?:RENAME|OPTIMIZE|BACKUP|RESTORE|CHECK|CHECKSUM|ANALYZE|REPAIR).*\s+TABLE'
    			. '|TRUNCATE(?:\s+TABLE)?'
    			. '|CREATE(?:\s+TEMPORARY)?\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?'
    			. '|ALTER(?:\s+IGNORE)?\s+TABLE'
    			. '|DROP\s+TABLE(?:\s+IF\s+EXISTS)?'
    			. '|CREATE(?:\s+\w+)?\s+INDEX.*\s+ON'
    			. '|DROP\s+INDEX.*\s+ON'
    			. '|LOAD\s+DATA.*INFILE.*INTO\s+TABLE'
    			. '|(?:GRANT|REVOKE).*ON\s+TABLE'
    			. '|SHOW\s+(?:.*FROM|.*TABLE)'
    		. ')\s+\(*\s*((?:[0-9a-zA-Z$_.`-]|[\xC2-\xDF][\x80-\xBF])+)\s*\)*/is',
    		$query,
    		$maybe
    	) ) {
    		return str_replace( '`', '', $maybe[1] );
    	}

    	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.1/src/wp-includes/class-wpdb.php#L3861)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/class-wpdb.php#L3861-L3929)

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

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

Checks if the query is accessing a collation considered safe.

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

Strips any invalid characters from the query.

  |

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

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

## User Contributed Notes

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