argie cruz
argie cruz

Reputation: 223

How To Avoid Queries in loops

Hi how can i avoid queries like these?

sample query:

$sql = DB::getInstance()->prepare("SELECT tb_id1 FROM table1 WHERE duedate < ?");
$sql->execute(array($currentdate));
  if($sql->rowCount()){
     while($row = $sql->fetch(PDO::FETCH_ASSOC)){      

   $sql2 = DB::getInstance()->prepare("UPDATE table2 SET isOverdue = 1 WHERE tb_id2 = ?");
   $sql2->execute(array($row["tb_id1"]));


    }
 }

Upvotes: 0

Views: 622

Answers (1)

Abhik Chakraborty
Abhik Chakraborty

Reputation: 44844

You can use update with join and thus by not using any loop in PHP

update table2 t2
join table1 t1 on t1.tb_id1 = t2.tb_id2
set t2.isOverdue = 1 
where 
t1.duedate < ?

Upvotes: 1

Related Questions