wppaste
WordPress

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

Since
4.2.0
Source
wp-includes/class-wpdb.php:3796
Finds the first table name referenced in a query.

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

$querystring
The query to search.

Return value

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

Performance profile

How much work a call to wpdb::get_table_from_query() 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
27–45

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 56.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

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_table_from_query() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
preg_match()27rtrim(), ltrim(), preg_match()
always31rtrim(), ltrim(), preg_match(), preg_match()
always39rtrim(), ltrim(), preg_match(), preg_match(), preg_match()
!preg_match()42–45rtrim(), ltrim(), preg_match(), preg_match(), preg_match(), preg_match()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5627–454
8.55627–454
8.45627–45415 fewer instructions than PHP 8.3
8.37136–544
8.27136–544
8.17136–544
7.47136–544

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.

Used by · 2

Source code

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

Changelog

Introduced in 4.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.9.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.