Mysqli Update Operation

Description

Update existing records in a table using associative array. Instead of writing long update queries, you needs to pass associative array of keys(columns) and values(update values) and associative array of conditions with keys as columns and value as column value.This function will automatically create query for you and updates data.

You need to set $parameter_types as data type of columns before using this function. For examples if you have five columns then you need to pass data type of each column like "sssis" where s denotes the string data type, i denotes numeric data type.


Function Reference :
	              void dbUpdate(string $table_name, assoc. array $update_array [, assoc. array $update_condition_array=array()])) 
              

Parameters

Name Type Required Parameter Info Example
table_name string Yes The name of the table to update old records any table name like employee_table,user_table
update_array array Yes Associative array with key as column name and values as column value. array("username"=>"abcd","gender"=>"male")
update_condition_array array Optional Associative array with key as column name and values as column value pass where condition to update old row. array("userid"=>15,"username"=>"abcd")

Examples

 <?php 
 $sdb= new SimplifiedDB(); 
 $sdb->dbConnect("localhost","username","password","dbuser");
 $sdb->parameter_types="ssi";
 $sdb->dbUpdate("user_roles",array("role_name"=>"Client","role_description"=>"Client"),array("roleid"=>28));        
 ?> 

Result

Old row will be updated in table.

Debuging

Debug Operation Result/Output
$sdb->query UPDATE user_roles SET `role_name`=?,`role_description`=? WHERE `roleid`=?
$sdb->last_insert_id
$sdb->rows_affected 1
$sdb->message_info Connected to database
$sdb->error_info

More Examples

 
          	 $updateRoles=array();
             $updateRoles["role_name"]="Manager";
             $updateRoles["role_description"]="Manages system";
             $whereRoles["roleid"]=15;
             $sdb->parameter_types="ssi";
             $sdb->dbUpdate("user_roles",$updateRoles,$whereRoles);