W3cubDocs

/WordPress

wpdb::update( string $table, array $data, array $where, string[]|string $format = null, string[]|string $where_format = null ): int|false

Updates a row in the table.

Description

Examples:

$wpdb->update(
    'table',
    array(
        'column1' => 'foo',
        'column2' => 'bar',
    ),
    array(
        'ID' => 1,
    )
);
$wpdb->update(
    'table',
    array(
        'column1' => 'foo',
        'column2' => 1337,
    ),
    array(
        'ID' => 1,
    ),
    array(
        '%s',
        '%d',
    ),
    array(
        '%d',
    )
);

See also

Parameters

$tablestringrequired
Table name.
$dataarrayrequired
Data to update (in column => value pairs).
Both $data columns and $data values 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.
$wherearrayrequired
A named array of WHERE clauses (in column => value pairs).
Multiple clauses will be joined with ANDs.
Both $where columns and $where values should be "raw".
Sending a null value will create an IS NULL comparison – the corresponding format will be ignored in this case.
$formatstring[]|stringoptional
An array of formats to be mapped to each of the values 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 $data will be treated as strings unless otherwise specified in wpdb::$field_types.

Default:null

$where_formatstring[]|stringoptional
An array of formats to be mapped to each of the values in $where.
If string, that format will be used for all of the items in $where.
A format is one of '%d', '%f', '%s' (integer, float, string).
If omitted, all values in $where will be treated as strings unless otherwise specified in wpdb::$field_types.

Default:null

Return

int|false The number of rows updated, or false on error.

Source

public function update( $table, $data, $where, $format = null, $where_format = null ) {
	if ( ! is_array( $data ) || ! is_array( $where ) ) {
		return false;
	}

	$data = $this->process_fields( $table, $data, $format );
	if ( false === $data ) {
		return false;
	}
	$where = $this->process_fields( $table, $where, $where_format );
	if ( false === $where ) {
		return false;
	}

	$fields     = array();
	$conditions = array();
	$values     = array();
	foreach ( $data as $field => $value ) {
		if ( is_null( $value['value'] ) ) {
			$fields[] = "`$field` = NULL";
			continue;
		}

		$fields[] = "`$field` = " . $value['format'];
		$values[] = $value['value'];
	}
	foreach ( $where as $field => $value ) {
		if ( is_null( $value['value'] ) ) {
			$conditions[] = "`$field` IS NULL";
			continue;
		}

		$conditions[] = "`$field` = " . $value['format'];
		$values[]     = $value['value'];
	}

	$fields     = implode( ', ', $fields );
	$conditions = implode( ' AND ', $conditions );

	$sql = "UPDATE `$table` SET $fields WHERE $conditions";

	$this->check_current_query = false;
	return $this->query( $this->prepare( $sql, $values ) );
}

Changelog

Version Description
2.5.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wpdb/update