wpdb::_insert_replace_helper( string $table, array $data, string[]|string $format = null, string $type = 'INSERT' ): int|false
- Since
- 3.0.0
- Source
wp-includes/class-wpdb.php:2590
Description
Runs an insert or replace query based on $type argument.
Compatibility
- WordPress
- since 3.0.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.
$dataarray- Data to insert (in column => value pairs).
Both$datacolumns and$datavalues should be "raw" (neither should be SQL escaped).
Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case. $formatstring[]|stringoptional- An array of formats to be mapped to each of the value in
$data.
If string, that format will be used for all of the values in$data.
A format is one of '%d', '%f', '%s' (integer, float, string).
If omitted, all values in$datawill be treated as strings unless otherwise specified in wpdb::$field_types. Default null.Default:null $typestringoptional- Type of operation. Either 'INSERT' or 'REPLACE'.
Default 'INSERT'.Default:'INSERT'
Return value
int|false- The number of rows affected, or false on error.
Performance profile
How much work a call to wpdb::_insert_replace_helper() 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
- 12–50
- Plugin surface
- None
- Called by
- 2
Reaches the database via ->query().
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 65.
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
- sqldatabase query
->query()called directly
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wpdb::_insert_replace_helper() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 12 | strtoupper() |
$data === false | 20 | strtoupper(), ->process_fields() |
$data !== false | 49–50 | strtoupper(), ->process_fields(), array_keys(), ->prepare(), ->query() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 65 | 12–50 | 5 | |
| 8.5 | 65 | 12–50 | 5 | |
| 8.4 | 65 | 12–50 | 5 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 71 | 12–56 | 5 | |
| 8.2 | 71 | 12–56 | 5 | |
| 8.1 | 71 | 12–56 | 5 | |
| 7.4 | 71 | 12–56 | 5 |
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 · 3
- wpdb::process_fields()Processes arrays of field/value pairs and field formats.
- wpdb::query()Performs a database query, using current database connection.
- wpdb::prepare()Prepares a SQL query for safe execution.
Used by · 2
- wpdb::insert()Inserts a row into the table.
- wpdb::replace()Replaces a row in the table or inserts it if it does not exist, based on a PRIMARY KEY or a UNIQUE index.
Source code
public function _insert_replace_helper( $table, $data, $format = null, $type = 'INSERT' ) { $this->insert_id = 0; if ( ! in_array( strtoupper( $type ), array( 'REPLACE', 'INSERT' ), true ) ) { return false; } $data = $this->process_fields( $table, $data, $format ); if ( false === $data ) { return false; } $formats = array(); $values = array(); foreach ( $data as $value ) { if ( is_null( $value['value'] ) ) { $formats[] = 'NULL'; continue; } $formats[] = $value['format']; $values[] = $value['value']; } $fields = '`' . implode( '`, `', array_keys( $data ) ) . '`'; $formats = implode( ', ', $formats ); $sql = "$type INTO `$table` ($fields) VALUES ($formats)"; $this->check_current_query = false; return $this->query( $this->prepare( $sql, $values ) ); }Changelog
Introduced in 3.0.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.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.