Mysql Update Operation

Description

Update existing records in a table using associative array. Instead of writing long update queries, you needs to pass 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.

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 associative array Yes Associative array with key as column name and values as column value. array("username"=>"abcd","gender"=>"male","age"=>29)
update_condition_array associative 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->dbUpdate("user_roles",array("role_name"=>"Editor","role_description"=>"Edit details"),array("roleid"=>12)); 
            ?> 
          

Result

Old row will be updated in table.

Debuging

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

More Examples

           //Example of using array variables to update operation
            $updateRoles=array();
            $updateRoles["role_name"]="Manager";
            $updateRoles["role_description"]="Manages system";
            $whereRoles["roleid"]=15;
            $sdb->dbUpdate("user_roles",$updateRoles,$whereRoles);
            
            //update operation on all rows as 'where condtion array' is not passed 
            $sdb->dbUpdate("user_roles",array("role_name"=>"Editor","role_description"=>"Edit posts"));