I don’t very much find too much information over the internet about PHP CLI so I am having a hard time figuring out how to finish my code.
Basically, the application should continue checking the MYSQL database every 2 seconds without exiting, unless otherwise the user entered the letter ‘q’.
I started it out by just printing the word ‘pro’ continuously before I implement MYSQL so my code looked like this:
<?php
fwrite(STDOUT, "This should print word 'pro' continuously\n");
fwrite(STDOUT, "\tto exit, simply press 'q' and enter\n");
do {
fwrite(STDOUT, "pro\n");
}while (fgetc(STDIN) != 'q');
?>
Pretty much when the user entered ‘q’, the app terminates, but the problem is it only prints out ‘pro’ once, and when I pressed enter.
fgetc()will block until there is data to read – in other words, when the script reaches thefgetc()call, the execution will halt until the user inputs something.In order to work around this, you will need to check whether there is any data to read using
stream_select(). You can also usestream_select()to limit the MySQL poll to every 2 seconds. A basic framework would look something like this:Note that it is likely that you will have to require your user to press
q + enterrather than justqbecause of the nature of the way these things work – and I don’t really understand why this is, maybe someone else can provide the missing piece here?