Category: >> PHP Code >> Update MySQL Table Using An Array Bookmark and Share

<< lastnext >>

Snippet Name: Update MySQL Table Using An Array

Description: Iterate over an array and update a mySQL table.

Comment: (none)

Author: CoderZone
Language: PHP
Highlight Mode: PHP
Last Modified: March 01st, 2009

// 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;
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© | users online: 5