Make WordPress Core


Ignore:
Timestamp:
10/13/2007 01:44:25 AM (19 years ago)
Author:
markjaquith
Message:

Allow wpdb->update() to optionally take a named array of where column/value pairs (which will be ANDed)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/wp-db.php

    r6238 r6239  
    267267         * @param string $table WARNING: not sanitized!
    268268         * @param array $data should not already be SQL-escaped
    269          * @param string $where_col the column of the WHERE statement.  WARNING: not sanitized!
     269         * @param mixed $where_col_or_array if a string, it represents the column of the WHERE statement.  If an array (named), it can represent multiple col = 'value' pairs that will be joined with ANDs  WARNING: the column names are not sanitized!
    270270         * @param string $where_val the value of the WHERE statement.  Should not already be SQL-escaped.
    271271         * @return mixed results of $this->query()
    272272         */
    273         function update($table, $data, $where_col, $where_val){
     273        function update($table, $data, $where_col_or_array, $where_val=NULL){
    274274                $data = add_magic_quotes($data);
    275                 $bits = array();
     275                $bits = $wheres = array();
    276276                foreach ( array_keys($data) as $k )
    277                         $bits[] = "`$k`='$data[$k]'";
    278                 $where_val = $this->escape($where_val);
    279                 return $this->query("UPDATE $table SET ".implode(', ',$bits)." WHERE $where_col = '$where_val' LIMIT 1");
     277                        $bits[] = "`$k` = '$data[$k]'";
     278
     279                if ( is_string( $where_col_or_array ) )
     280                        $wheres = array( "$where_col_or_array = '" . $this->escape($where_val) . "'" );
     281                elseif ( is_array( $where_col_or_array ) )
     282                        foreach ( $where_col_or_array as $c => $v )
     283                                $wheres[] = "$c = '" . $this->escape( $v ) . "'";
     284                else
     285                        return false;
     286                return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) . ' LIMIT 1' );
    280287        }
    281288
Note: See TracChangeset for help on using the changeset viewer.