-2

I want to create service which every second checking incoming orders in database and doing some stuff. Now I'm using php5.6 and mysql but I'm not sure this is good for that. Sorry for my code, but now I'm doing like this:

 for ($x=0; $x < 999999999999; $x++)
 {



$query1 = mysql_query("SELECT * FROM `orders`");
while ($row = mysql_fetch_assoc($query1)) {
//doing some stuff here
}
sleep(1);
}

It works about 12 hours and when crashing with error. Is there better solution for that or maybe is better to choose different language ?

TheEldrone
  • 87
  • 1
  • 11
  • 4
    you can use while(true){} – Sugumar Venkatesan Jun 14 '18 at 16:59
  • 2
    FYI, [you shouldn't use `mysql_*` functions in new code](http://stackoverflow.com/questions/12859942/). They are no longer maintained [and are officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). See the [red box](http://php.net/manual/en/function.mysql-connect.php)? Learn about [*prepared statements*](https://en.wikipedia.org/wiki/Prepared_statement) instead, and use [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli) - [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you decide which one is best for you. – John Conde Jun 14 '18 at 16:59
  • 3
    why are you checking the incoming orders by querying the database everytime, if it's a same server, run the code when the order comes – Sugumar Venkatesan Jun 14 '18 at 17:00
  • It could also be due to memory... if you are not careful with your code, then you could just be running out of memory as your script runs infinitely – Ice76 Jun 14 '18 at 17:05
  • Do your stuff when you receive the order and if its really not possible you can use triger in db to invoke php code but its not easy. https://stackoverflow.com/a/1467387/6281135 – USER249 Jun 14 '18 at 17:12
  • Thanks to all, I got it, php is usless for that. I think to try node.js instead. – TheEldrone Jun 14 '18 at 17:21

1 Answers1

1

Your 999999999999 will get reached.

Try something like this:

<?php

    $link = mysqli_connect("localhost", "username", "password", "database");
    if(!$link) {
        die("Could not connect to MySQL database.");
    }
    while(1) {
        $query = "SELECT * FROM YOUR_TABLE";
        $res = mysqli_query($link, $query);
        if(!$res) {
            die("Could not execute query: " . mysqli_error($link));
        }
        while($row = mysqli_fetch_assoc($res)) {
            // Do stuff here
        }
        sleep(1);
    }