Search code examples
mysqlsleep

Mysql select sleep then return


I want select X records from database (in PHP script), then sleep 60 seconds after continue the next 60 results...

SO:

SELECT * FROM TABLE WHERE A = 'B' LIMIT 60
SELECT SLEEP(60);
....
SELECT * FROM TABLE WHERE A = 'B' LIMIT X **where X is the next 60 results, then**
SELECT SLEEP(60);

AND etc...

How can I achievement this?


Solution

  • In PHP:

    <?php
    // obtain the database connection, there's a heap of examples on the net, assuming you're using a library like mysqlite
    $offset = 0;
    while (true) {
        if ($offset == 0) {
            $res = $db->query('SELECT * FROM TABLE WHERE A = 'B' LIMIT 60');
        } else {
            $res = $db->query('SELECT * FROM TABLE WHERE A = 'B' LIMIT ' . $offset . ',60');
        }
        $rows = $db->fetch_assoc($res);
        sleep(60);
        if ($offset >= $some_arbitrary_number) {
            break;
        }
        $offset += 60;
    }
    

    What you're doing is gradually incrementing the limit field by 60 until you reach a limit. The easiest way to do it is in a control while loop using true for the condition and break when you reach your invalid condition.