wpdb::update PHP Method

update() public method

wpdb::update( 'table', array( 'column' => 'foo', 'field' => 'bar' ), array( 'ID' => 1 ) ) wpdb::update( 'table', array( 'column' => 'foo', 'field' => 1337 ), array( 'ID' => 1 ), array( '%s', '%d' ), array( '%d' ) )
See also: wpdb::prepare()
See also: wpdb::$field_types
See also: wp_set_wpdb_vars()
Since: 2.5.0
public update ( string $table, array $data, array $where, array | string $format = null, array | string $where_format = null ) : integer | false
$table string table name
$data array Data to update (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped).
$where array 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".
$format array | string Optional. 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.
$where_format array | string Optional. 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.
return integer | false The number of rows updated, or false on error.
    public function update($table, $data, $where, $format = null, $where_format = null)
    {
        if (!is_array($data) || !is_array($where)) {
            return false;
        }
        $formats = $format = (array) $format;
        $bits = $wheres = [];
        foreach ((array) array_keys($data) as $field) {
            if (!empty($format)) {
                $form = ($form = array_shift($formats)) ? $form : $format[0];
            } elseif (isset($this->field_types[$field])) {
                $form = $this->field_types[$field];
            } else {
                $form = '%s';
            }
            $bits[] = "`{$field}` = {$form}";
        }
        $where_formats = $where_format = (array) $where_format;
        foreach ((array) array_keys($where) as $field) {
            if (!empty($where_format)) {
                $form = ($form = array_shift($where_formats)) ? $form : $where_format[0];
            } elseif (isset($this->field_types[$field])) {
                $form = $this->field_types[$field];
            } else {
                $form = '%s';
            }
            $wheres[] = "`{$field}` = {$form}";
        }
        $sql = "UPDATE `{$table}` SET " . implode(', ', $bits) . ' WHERE ' . implode(' AND ', $wheres);
        return $this->query($this->prepare($sql, array_merge(array_values($data), array_values($where))));
    }

Usage Example

Example #1
0
 /**
  * Update taxonomy meta
  *
  * @param int $term_id
  * @param string $key
  * @param mixed $value
  * @return int
  */
 public function update($term_id, $key, $value)
 {
     $old_value = $this->get($term_id, $key);
     if ($old_value === false) {
         return $this->_add($term_id, $key, $value);
     }
     if ($old_value === $value) {
         return true;
     }
     wp_cache_set($this->_get_cache_key($term_id, $key), $value, 'taxonomy-meta');
     return $this->_db->update($this->_table, ['meta_value' => maybe_serialize($value)], ['term_id' => $term_id, 'meta_key' => $key]);
 }
All Usage Examples Of wpdb::update