I googled a lot the last days and a few times i´ve seen postings that say that the $wpdb->update
function doens´t support multiple updates and that this is not well documented. -> Sorry i don´t have links anymore…
I think that this is true because i couldn´t get it to work and i found not a single example witch works this way. So i use this way now to update multiple rows and still be able to handle errors.
The function:
function update_queries( $queries ) {
global $wpdb;
// set array
$error = array();
// run update commands
foreach( $queries as $query ) {
$query = str_replace( '[wp-prefix]', $wpdb->prefix, $query );
$last_error = $wpdb->last_error;
$wpdb->query( $query );
// fill array when we have an error
if( (empty( $wpdb->result ) || !$wpdb->result ) && !empty( $wpdb->last_error ) && $last_error != $wpdb->last_error ) {
$error[]= $wpdb->last_error." ($query)";
}
}
// when we have an error
if( $error ) {
return $error;
// when everything is fine
}else{
return false;
}
}
If we want to update a few things:
// update database
$queries = array();
$queries[] = "UPDATE `[wp-prefix]table` SET `value` = '$value' WHERE `name` = 'name';";
$queries[] = "UPDATE `[wp-prefix]table` SET `value` = '$value2' WHERE `name` = 'name2';";
$error = update_queries( $queries );
// if we have an error
if( !empty( $error ) ) {
.....
// when everything is fine
}else{
.....
}