Jump to content

[SOLVED] Combine and Optimize MYSQL UPDATE with PHP


mikenl

Recommended Posts

Hi,

 

I want to set all fields in a MySQL row to the default value (0) and update the row with new values.

 

I now use 2 queries and I'm curious if these can be combined and/or optimized?

 


// set prof_pref to 0

$setfetish0 = "UPDATE prof_pref SET ";

for ($N=1; $N<=12; $N++) 
    {     
$setpref0 = $setpref0 . "pref".$N." = '0'";
if($N != 12){
	$setpref0 = $setpref0 . ", ";
	} 
  } 	

$setpref0 = $setpref0 . " WHERE unid = '$session_id'";

mysql_query($setpref0 );


// update prof_pref 

$pref= $_POST[pref];

dbsystem();	
$setpref= "UPDATE prof_pref SET ";	

foreach ($pref as $pre=> $value){
if(!empty($pre)){
	$setpref= $setpref. ", ";
	}  
$setpref= $setpref. "pref".$value." = '1'";
 }

$setpref= $setpref. " WHERE unid = '$session_id'";

mysql_query($setpref);

if you are pertaining to the same info and update them consecutively, i dont think its a good idea since you are going to update the same fields. why not go directly to the second update instead? or do you have other special implication.

 

sorry but i dont really see any good reason for doing such.

OK, messed around with the query, and this is now working:

 

dbsystem();	
$setlanguage = "UPDATE prof_language SET ";	

foreach ($language as $lang => $value){
if(!empty($language)){
	$setlanguage = $setlanguage. ", ";
	}  
$setlanguage = $setlanguage. "lang".$value." = '$lang'";
 }

$setlanguage = $setlanguage. " WHERE unid = '$session_id'";

mysql_query($setlanguage);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.