How To Avoid Queries in loops

argie cruz

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"]));


    }
 }
Abhik Chakraborty

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 < ?

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to avoid N+1 queries in Rails "each" loops?

From Dev

How to Avoid Conditionals in Loops

From Dev

How to avoid loops for sparse Matrix?

From Dev

How to avoid for-loops in R

From Dev

How to avoid loops for sparse Matrix?

From Dev

for loops with callback functions in jQuery or how to avoid them

From Java

How can I avoid complex for loops?

From Java

How to avoid for loops and let statement for this program

From Dev

How to avoid loops in the exceptions of generic regex rules?

From Dev

How does Perl avoid shebang loops?

From Dev

How to avoid nested for-each loops?

From Dev

R: How to avoid 2 'for' loops in R in this function

From Dev

for loops with callback functions in jQuery or how to avoid them

From Dev

Sequences and patterns: how to avoid loops in R

From Dev

How to avoid for loops in this code to improve computation time

From Dev

How to avoid nested for loops in this particular case?

From Dev

jQuery how to create loops to avoid repetition

From Dev

How to avoid multiple repeat database queries

From Dev

How to avoid excess queries in django template?

From Dev

How to avoid N+1 queries with Ransack

From Dev

Django conditional queries: How can I avoid this?

From Dev

how to use nested foreach loops in php using mysql queries

From Dev

how to create loops with multi relation taxanomy queries in wordpress

From Java

How can I avoid "for" loops with an "if" condition inside them with C++?

From Dev

How to write Java for loops to avoid repeatedly computing the upper bound

From Dev

R - How to avoid loops when comparing two datasets?

From Dev

How to avoid loops in Drools without using NO-LOOP attribute?

From Dev

How to avoid loops for specifying limits for Pandas Dataframe filtering?

From Dev

How to avoid infinite loops when counting cluster size in a graph?

Related Related

  1. 1

    How to avoid N+1 queries in Rails "each" loops?

  2. 2

    How to Avoid Conditionals in Loops

  3. 3

    How to avoid loops for sparse Matrix?

  4. 4

    How to avoid for-loops in R

  5. 5

    How to avoid loops for sparse Matrix?

  6. 6

    for loops with callback functions in jQuery or how to avoid them

  7. 7

    How can I avoid complex for loops?

  8. 8

    How to avoid for loops and let statement for this program

  9. 9

    How to avoid loops in the exceptions of generic regex rules?

  10. 10

    How does Perl avoid shebang loops?

  11. 11

    How to avoid nested for-each loops?

  12. 12

    R: How to avoid 2 'for' loops in R in this function

  13. 13

    for loops with callback functions in jQuery or how to avoid them

  14. 14

    Sequences and patterns: how to avoid loops in R

  15. 15

    How to avoid for loops in this code to improve computation time

  16. 16

    How to avoid nested for loops in this particular case?

  17. 17

    jQuery how to create loops to avoid repetition

  18. 18

    How to avoid multiple repeat database queries

  19. 19

    How to avoid excess queries in django template?

  20. 20

    How to avoid N+1 queries with Ransack

  21. 21

    Django conditional queries: How can I avoid this?

  22. 22

    how to use nested foreach loops in php using mysql queries

  23. 23

    how to create loops with multi relation taxanomy queries in wordpress

  24. 24

    How can I avoid "for" loops with an "if" condition inside them with C++?

  25. 25

    How to write Java for loops to avoid repeatedly computing the upper bound

  26. 26

    R - How to avoid loops when comparing two datasets?

  27. 27

    How to avoid loops in Drools without using NO-LOOP attribute?

  28. 28

    How to avoid loops for specifying limits for Pandas Dataframe filtering?

  29. 29

    How to avoid infinite loops when counting cluster size in a graph?

HotTag

Archive