Array ( [0] => fieldname1 [1] => fieldname2 ... ) [table2] => Array ( [0] => fieldname3 [1] => fieldname4 [2] => fieldname5 ... ) ... Now we need to run through those tables one at a time and convert them */ $errorTally = 0; echo "Updating records:\n"; foreach($fields as $tableName => $fieldSet){ // build the query that gives us the field values we need to update in this table, as well as the primary keys $query = "SELECT `" . implode('`, `', $fieldSet) . '`'; for($n = 0; $n < count($keys[$tableName]); $n++){ $query .= ", `" . $keys[$tableName][$n] . "` AS __KEYFIELD_" . ($n + 1) . "__"; } $query .= " FROM $tableName"; // fetch all of those values $updates = array(); $data = mysql_query($query); while($record = mysql_fetch_array($data)){ $updates[] = $record; } // now re-insert those values into the table foreach($updates as $update){ $query = "UPDATE $tableName SET"; $useComma = false; foreach($fieldSet as $fieldName){ $fieldValue = $update[$fieldName]; if($useComma){ $query .= ","; }else{ $useComma = true; } $newValue = mb_convert_encoding($fieldValue, "UTF-8", "iso-8859-1"); $query .= sprintf(" `%s` = '%s'", $fieldName, mysql_real_escape_string($newValue)); } $query .= " WHERE "; for($n = 0; $n < count($keys[$tableName]); $n++){ if($n > 0) $query .= " AND "; $query .= "`" . $keys[$tableName][$n] . "` = '" . mysql_real_escape_string($update["__KEYFIELD_" . ($n + 1) . "__"]) . "'"; } $success = mysql_query($query); if($success) echo '.'; else{ echo "\nFailed to execute query: $query\n"; $errorTally ++; } } unset($updates); } echo "\nComplete with $errorTally failed queries.\n"; // now drop the id column that we added to the committees_link table $query = "ALTER TABLE `committees_link` DROP `id`"; mysql_query($query); }