Hello, 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 UPDATE
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?
<?
// 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);
}
?>
Should I use a while
or a for
loop?
Is this code valid for multiple UPDATEs or does exist something better like specific queries for this kind of action?