Updates a row in the table.
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',
)
); $tablestringrequired
$dataarrayrequired
$wherearrayrequired
$formatstring[]|stringoptional
'%d', '%f', '%s' (integer, float, string).Default:null
$where_formatstring[]|stringoptional
'%d', '%f', '%s' (integer, float, string).Default:null
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 ) );
}
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wpdb/update