Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

CornerBLUE, Inc. http://www.cornerblue.com

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

PHP: Update MySQL Table Using An Array

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

   1  
   2  function mysql_update_array($table, $data, $id_field, $id_value) {
   3  	foreach ($data as $field=>$value) {
   4  		$fields[] = sprintf("`%s` = '%s'", $field, mysql_real_escape_string($value));
   5  	}
   6  	$field_list = join(',', $fields);
   7  	
   8  	$query = sprintf("UPDATE `%s` SET %s WHERE `%s` = %s", $table, $field_list, $id_field, intval($id_value));
   9  	
  10  	return $query;
  11  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS