Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8332943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:51:57+00:00 2026-06-09T02:51:57+00:00

I don’t very much find too much information over the internet about PHP CLI

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:51:58+00:00Added an answer on June 9, 2026 at 2:51 am

    fgetc() will block until there is data to read – in other words, when the script reaches the fgetc() 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 use stream_select() to limit the MySQL poll to every 2 seconds. A basic framework would look something like this:

    <?php
    
      // Do all your init work here, connect to DB etc
      $tickerSecs = 2;
      echo "Hello! I've started\n";
    
      do {
    
        // Do actual work here
        echo "I'm polling the database\n";
    
        // See stream_select() docs for an explanation of why this is necessary
        $r = array(STDIN);
        $w = $e = NULL;
        if (stream_select($r, $w, $e, $tickerSecs) > 0) {
          // The user input something
          echo "You input something\n";
          $char = fread(STDIN, 1);
          if ($char == 'q') {
            // The user pressed 'q'
            echo "You told me to quit\n";
            break;
          } else {
            echo "I don't understand '$char'\n";
          }
        }
    
      } while (TRUE); // Loop forever
    
      // Do shutdown work here
      echo "I'm shutting down\n";
    

    Note that it is likely that you will have to require your user to press q + enter rather than just q because 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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't have much knowledge about the IPv6 protocol, so sorry if the question
Don't know much about encryption... Say I'm preparing a SAML request to submit to
Don't know much about running a function on every item in an array, still
don't know if the title describes anything about what I'm trying to say but
Don't overlook the 'date AND TIME' part though.
Don't think that I'm mad, I understand how php works! That being said. I
Don't be frightened, its a very basic code. Just wanted to check with you
Don't know whats exactly going on, but it's definitely killing my time for nothing.
Don't overthink this - there's a very commonly used term and I ... have
Don't have much to say, just can get into the event handler. XAML: <Grid>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.