How to solve my sluggish code (using PHP - PDO )

danihandiki

I am using PHP PDO, this the code :

for ($i = 0; $i < 1000; $i++) {
  //select room and insert to chart
  $thisDay=date ("Y-m-d");
  $query = $db->prepare("SELECT room FROM d_room WHERE activ=1 ORDER BY room");
  $query->execute();
  for($a = 1; $result = $query->fetch(); $a++) {
    $query2 = $db->prepare("INSERT INTO d_chart (date,room,status) VALUES (? + INTERVAL ? DAY,?,0)");
    $query2->execute(array($thisDay,$i,$result['room']));
  }
}

this code run too slow, How to make better code and fast, < 2 second.

Phil

You should be able to make this significantly simpler by using an INSERT...SELECT query (assuming MySQL)

// Make sure you see any errors that might occur
ini_set('display_errors', 'On');
error_reporting(E_ALL);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$thisDay = date('Y-m-d');
$stmt = $db->prepare('INSERT INTO d_chart (date, room, status)
    SELECT (:thisDay + INTERVAL :days DAY), room, 0
    FROM d_room WHERE activ = 1'); // there's no need to order these results
$stmt->bindParam(':thisDay', $thisDay);
$stmt->bindParam(':days', $i, PDO::PARAM_INT);
for ($i = 0; $i < 1000; $i++) {
    $stmt->execute();
}

If d_room.activ has an index, this would be even faster.

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 solve a very sluggish Windows 10 UI

From Dev

How to solve SonarQube complaints about my code?

From Dev

How to solve SonarQube complaints about my code?

From Dev

My boot menu is sluggish

From Dev

How do I solve this undefined index error when using PHP to query my MySQL Database?

From Dev

How to notice MySQL errors using PDO in PHP

From Dev

How to properly delete a row using PHP & PDO

From Dev

How to show table values in php using PDO

From Dev

how to fix a sluggish mouse?

From Dev

Refactoring code in OOP PHP and using PDO for mysql queries

From Dev

How to solve the Total of my html code that linking with input type = "number"

From Dev

My code is leaking. How can I solve it?

From Dev

How to solve the Total of my html code that linking with input type = "number"

From Dev

PHP sql using PDO

From Dev

Using php functions with PDO

From Dev

Using MySQL IN() with PHP and PDO

From Dev

How to submit HTML form using TinyMCE, jQuery and PDO? (full code)

From Dev

PHP Image Display using my code

From Dev

How to diagnose a sluggish or unresponsive machine

From Dev

How to connect to MySQL using PHP PDO in easyphp 16.1.1

From Dev

How to turn off ANSI_WARNINGS using PDO in PHP

From Dev

How to create a load more button using php, pdo and oop

From Dev

how to clear form using php PDO after successful submission

From Dev

How to get info from a child table using PDO in PHP with SQL?

From Dev

How to turn off ANSI_WARNINGS using PDO in PHP

From Dev

How to get the insert ID after insert using the PDO transactino in php

From Dev

How to pass multiple parameters to stored procedure using pdo in php

From Dev

How can I query two tables using php PDO?

From Dev

How to insert unknown amount of colums in mysql using PHP PDO

Related Related

  1. 1

    How to solve a very sluggish Windows 10 UI

  2. 2

    How to solve SonarQube complaints about my code?

  3. 3

    How to solve SonarQube complaints about my code?

  4. 4

    My boot menu is sluggish

  5. 5

    How do I solve this undefined index error when using PHP to query my MySQL Database?

  6. 6

    How to notice MySQL errors using PDO in PHP

  7. 7

    How to properly delete a row using PHP & PDO

  8. 8

    How to show table values in php using PDO

  9. 9

    how to fix a sluggish mouse?

  10. 10

    Refactoring code in OOP PHP and using PDO for mysql queries

  11. 11

    How to solve the Total of my html code that linking with input type = "number"

  12. 12

    My code is leaking. How can I solve it?

  13. 13

    How to solve the Total of my html code that linking with input type = "number"

  14. 14

    PHP sql using PDO

  15. 15

    Using php functions with PDO

  16. 16

    Using MySQL IN() with PHP and PDO

  17. 17

    How to submit HTML form using TinyMCE, jQuery and PDO? (full code)

  18. 18

    PHP Image Display using my code

  19. 19

    How to diagnose a sluggish or unresponsive machine

  20. 20

    How to connect to MySQL using PHP PDO in easyphp 16.1.1

  21. 21

    How to turn off ANSI_WARNINGS using PDO in PHP

  22. 22

    How to create a load more button using php, pdo and oop

  23. 23

    how to clear form using php PDO after successful submission

  24. 24

    How to get info from a child table using PDO in PHP with SQL?

  25. 25

    How to turn off ANSI_WARNINGS using PDO in PHP

  26. 26

    How to get the insert ID after insert using the PDO transactino in php

  27. 27

    How to pass multiple parameters to stored procedure using pdo in php

  28. 28

    How can I query two tables using php PDO?

  29. 29

    How to insert unknown amount of colums in mysql using PHP PDO

HotTag

Archive