Basics rules on UPDATE statement for relational MySql table: for vs while

unknown25

New Member
I'm doing my first steps with mysql and php, so I have doubts on foundamental rules for a right code optimization.I have a case where my \[code\]UPDATE\[/code\] statement need to be executed on a certain number of rows, because it should be executed on a relational table, so is a for cicle correct?\[code\]<?// connection already created$data[] = array ("id" => 54, "enabled" => 1);$data[] = array ("id" => 33, "enabled" => 0);$data[] = array ("id" => 12, "enabled" => 0);$data[] = array ("id" => 58, "enabled" => 0);$data[] = array ("id" => 21, "enabled" => 1);$data[] = array ("id" => 10, "enabled" => 1);$data[] = array ("id" => 18, "enabled" => 0);$data[] = array ("id" => 32, "enabled" => 1);$data[] = array ("id" => 84, "enabled" => 0);$data[] = array ("id" => 80, "enabled" => 1);for (var $i = 0; $i < count ($data); $i ++) { $id = $data[$i]["id"]; $enabled = $data[$i]["enabled"]; $sql = "UPDATE users SET user_enabled = '$enabled' WHERE user_id = '$id' LIMIT 1;"; $res = mysql_query ($sql); $num = mysql_num_rows ($res);}?>\[/code\]Should I use a \[code\]while\[/code\] or a \[code\]for\[/code\] loop?Is this code valid for multiple UPDATEs or does exist something better like specific queries for this kind of action?
 
Back
Top