Simple Update

  global $wpdb;
  $sql = $wpdb->prepare("UPDATE {$wpdb->prefix}tbl_my_table SET
						my_fieldname1 = 123,
						my_fieldname2 = %s
					WHERE indexer = %d
					", 'Bert', $some_value);
  $wpdb->query($sql);
If you need to know how many rows were affected
  $Count = $wpdb->query($sql);
  
  if ($Count > 0)
    return(True);
  else
    return(False);

Update From Row + Value Array

/*
      $FieldNamesValuesArray = array();
      $FieldNamesValuesArray['ColumnName1'] = $ColumnName1;
      $FieldNamesValuesArray['ColumnName2'] = $ColumnName2;
      db_user_write_fields($user_id, $FieldNamesValuesArray);
*/
function db_user_write_fields($user_id, $FieldNameValueArray)
{
  global $wpdb;
  
  $sql = "UPDATE {$wpdb->prefix}tbl_my_table SET ";

  $FirstField = true;
  foreach ($FieldNameValueArray as $key => $value)
  {
    if ($FirstField)
      $sql .= $wpdb->prepare(" $key = %s ", $value);
    else
      $sql .= $wpdb->prepare(", $key = %s ", $value);
    $FirstField = false;
  }
  
  $sql .= $wpdb->prepare(" WHERE user_id = %d", $user_id);

  $wpdb->query($sql);
  
}
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published.