jeudi 7 mai 2015

Periodically query sqlite from php

I have an sqlite database that I query from PHP periodically. The query is always the same and it returns me a string. Once the string changes in the database the loop ends. The following code is working, but I am pretty sure this is not the optimal way to do this...

                <?php

                    class MyDB extends SQLite3
                    {
                          function __construct()
                          {
                             $this->open('db.sqlite');
                          }
                    }
                    $loop=True;
                    while($loop==True){
                            sleep(10);
                            $db = new MyDB();
                            if(!$db){
                                  echo $db->lastErrorMsg();
                            } else {
                                    echo "Opened database successfully\n";
                            }
                            $sql =<<<EOF
                            SELECT status from t_jobs WHERE name='$file_name';
EOF;

                            $ret = $db->query($sql);
                            $state =$ret->fetchArray(SQLITE3_ASSOC);
                            $output = (string)$state['status'];
                            if(strcmp($output, 'FINISHED')==0){
                                    $loop=False;
                            }
                            echo $output;
                            $db->close();
                    }

            ?>

Aucun commentaire:

Enregistrer un commentaire