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

git-svn-id: https://develop.svn.wordpress.org/trunk@6239 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Mark Jaquith 2007-10-13 01:44:25 +00:00
parent 48c555d637
commit 05f7b9d773
1 changed files with 13 additions and 6 deletions

View File

@ -266,17 +266,24 @@ class wpdb {
* Update a row in the table with an array of data * Update a row in the table with an array of data
* @param string $table WARNING: not sanitized! * @param string $table WARNING: not sanitized!
* @param array $data should not already be SQL-escaped * @param array $data should not already be SQL-escaped
* @param string $where_col the column of the WHERE statement. WARNING: not sanitized! * @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!
* @param string $where_val the value of the WHERE statement. Should not already be SQL-escaped. * @param string $where_val the value of the WHERE statement. Should not already be SQL-escaped.
* @return mixed results of $this->query() * @return mixed results of $this->query()
*/ */
function update($table, $data, $where_col, $where_val){ function update($table, $data, $where_col_or_array, $where_val=NULL){
$data = add_magic_quotes($data); $data = add_magic_quotes($data);
$bits = array(); $bits = $wheres = array();
foreach ( array_keys($data) as $k ) foreach ( array_keys($data) as $k )
$bits[] = "`$k` = '$data[$k]'"; $bits[] = "`$k` = '$data[$k]'";
$where_val = $this->escape($where_val);
return $this->query("UPDATE $table SET ".implode(', ',$bits)." WHERE $where_col = '$where_val' LIMIT 1"); if ( is_string( $where_col_or_array ) )
$wheres = array( "$where_col_or_array = '" . $this->escape($where_val) . "'" );
elseif ( is_array( $where_col_or_array ) )
foreach ( $where_col_or_array as $c => $v )
$wheres[] = "$c = '" . $this->escape( $v ) . "'";
else
return false;
return $this->query( "UPDATE $table SET " . implode( ', ', $bits ) . ' WHERE ' . implode( ' AND ', $wheres ) . ' LIMIT 1' );
} }
/** /**