1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP output while executing script

Discussion in 'PHP' started by Blutarsky, May 13, 2010.

  1. #1
    I'm processing some MySQL data, scanning a whole DB, and as the processing is quite long, I'd like to output a counter to show progress.
    The problem is that output will not happen until processing has finished.
    I have tried the Ajax asynchronous load, calling an external PHP scirpt but it doesn't work propbabily because the main script did not end.
    Any hint?

    Example:

    
    function processdb(){
    $link = mysqli_connect("localhost", "root", "") or die("Could not connect: " . mysqli_error());
    mysqli_select_db($link, "mydb");
    
    $querystr = "SELECT * FROM tj_posts";
    $result = mysqli_query($link, $querystr);
    
    while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) 
    	{
    	        // process db here
    		echo "Some output here"; // need a trick to flush data here!!!!!!!!!!!!!
    		}
    mysqli_free_result($result);
    echo "Done!";
    die;
    }
    Code (markup):
     
    Blutarsky, May 13, 2010 IP
  2. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You have to flush your output buffer.

    Just toss in the following code after each mysql query:

    ob_flush();
    PHP:
    That will force PHP to print everything to the screen that it has queued up to print. So if you run that function after each query it will do what you want.

    See the documentation on it here: http://php.net/manual/en/function.ob-flush.php
     
    Christian Little, May 13, 2010 IP
    Blutarsky and sarahk like this.
  3. Blutarsky

    Blutarsky Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes! It works! Thanks!
     
    Blutarsky, May 13, 2010 IP
  4. Blutarsky

    Blutarsky Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Christian, one more question:
    is there a simple way to clear the page contents before every call? That would allow keeping output content on top of page.....
     
    Blutarsky, May 14, 2010 IP
  5. softwareengg07

    softwareengg07 Member

    Messages:
    106
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    26
    #5
    you need to flush out your output buffer.this solve your problem.
     
    softwareengg07, May 14, 2010 IP
  6. Blutarsky

    Blutarsky Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It would be nice if you could post a sample. This calls/order seems very tricky....
     
    Blutarsky, May 14, 2010 IP
  7. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Clearing the screen would depend on how you are running your PHP script, as most consoles have a clearscreen command you could just run (i.e. putty/linux system I think have "clrscr" or something like that.

    Here's a rough example:

    
    
    function processdb(){
    $link = mysqli_connect("localhost", "root", "") or die("Could not connect: " . mysqli_error());
    mysqli_select_db($link, "mydb");
    
    $querystr = "SELECT * FROM tj_posts";
    $result = mysqli_query($link, $querystr);
    
    while ($row = mysqli_fetch_array($result, MYSQL_ASSOC)) 
    	{
    		exec("clrscr");  // PHP will run the console command clrscr or whatever it is that clears the screren
    	        // process db here
    		echo "Some output here"; 
    		ob_flush();  // this will tell PHP to print that echo statement immediately to your screen		
    		}
    mysqli_free_result($result);
    echo "Done!";
    die;
    }
    
    PHP:
    Exec function runs a prompt command, so any command that you can run on your computer can be run with it, documentation: http://php.net/manual/en/function.exec.php

    If you can't figure out the console command to clear the screen, here's an alternative:

    Add a few line breaks, ie:

    
    	        // process db here
    		echo "Some output here\n\n"; 
    		ob_flush();  // this will tell PHP to print that echo statement immediately to your screen	
    PHP:
    That will add 2 line breaks between each echo statement. If you're running this code in a web browser, change the \n to <br /> instead.

    That's what I do, so I can see everything that happens in the order it happens. Makes it way easier to debug than wiping out the screen.

    Alternative 2: If you're doing this in a web browser you could do it with Javascript and the document.write function and having it output to a specific div or textarea.
     
    Christian Little, May 14, 2010 IP
  8. Blutarsky

    Blutarsky Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks! (tried to increase your rep. but would not allow :-(
     
    Blutarsky, May 14, 2010 IP
  9. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That's because you can't increase somebody's rep twice in a row. You have to give somebody else rep before you could give it to me again (as least I assume it was you that gave me rep for this thread already lol).
     
    Christian Little, May 14, 2010 IP
  10. media143

    media143 Member

    Messages:
    238
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    28
    #10
    I tried this script and working fine on my Apache. Just use ob_flush() this function if you are facing problem again.
     
    media143, May 14, 2010 IP
  11. Blutarsky

    Blutarsky Peon

    Messages:
    28
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Exactly!! :)
     
    Blutarsky, May 14, 2010 IP