Snippet Name: Update MySQL Table Using An Array
Description: Iterate over an array and update a mySQL table.
Comment: (none)
Language: PHP, MYSQL
Highlight Mode: PHP
Last Modified: March 01st, 2009
|
<?PHP
// Parameters:
// Table: Name of table to update
// Data: array of $field->$value with new values
// Id Field: Name of field to use as ID field
// Id Value: Value of ID field
FUNCTION mysql_update_array($table, $data, $id_field, $id_value) {
FOREACH ($data AS $field=>$value) {
$fields[] = SPRINTF("`%s` = '%s'", $field, mysql_real_escape_string($value));
}
$field_list = JOIN(',', $fields);
$query = SPRINTF("UPDATE `%s` SET %s WHERE `%s` = %s", $table, $field_list, $id_field, INTVAL($id_value));
RETURN $query;
}
?> |