Solved

PHP flush() not working to flush output to browser before script is done

Posted on 2007-12-05
11
2,424 Views
Last Modified: 2010-04-21
My script is a basic PHP script calling data from a MYSQL database
but it takes about 2 minutes to run the script as it emails out 1000 people on an email list
I want it to display the email address on screen AS it emails each one instead of all at once at the end of 2 minutes lists them all

I have tried adding

ob_implicit_flush();

at the top, no. that does not work.

and

ob_flush();
flush();

after every echo of the email address to the screen.

Still doesn't work.

help please.

0
Comment
Question by:brianharrell
  • 8
  • 3
11 Comments
 
LVL 5

Expert Comment

by:nacker2000
ID: 20416446
Hi,

So you your code is somethings like:
<?php
	//DO QUERY TO GET EMAILS
	
	//loop through records
	while($rst = mysql_fetch_array($result)){
		//get email address
		$email = trim($rst['email']);
		//send mail and check it was sent
		if(mail($email, 'Subject', 'Message', 'From:you@yourdomain.com')){
			//ouptut to screen
			print('Sent email to: ' . $email);
		}
	}
?>

Open in new window

0
 

Author Comment

by:brianharrell
ID: 20416477
my code say echo instead of print but very much the same.


right after

                        print('Sent email to: ' . $email);


I put the flush() code but ob_flush() and flush() don't seem to output to browser except when the whole script is done (over 1000 emails)
0
 
LVL 5

Accepted Solution

by:
nacker2000 earned 200 total points
ID: 20418262
Have you tried pausing the script? You could try sleeping the script for a faction of a second after the echo. However, this will make the script even slower if it works because you would be waiting X number of milliseconds on each email.
<?php
	//DO QUERY TO GET EMAILS
	
	//loop through records
	while($rst = mysql_fetch_array($result)){
		//get email address
		$email = trim($rst['email']);
		//send mail and check it was sent
		if(mail($email, 'Subject', 'Message', 'From:you@yourdomain.com')){
			//ouptut to screen
			print('Sent email to: ' . $email);
			// wait for 100 milliseconds (1 second = 1000000)
			usleep(100000);
		}
	}
?>

Open in new window

0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

Author Comment

by:brianharrell
ID: 20424146
I'll test that shortly.
0
 
LVL 5

Expert Comment

by:nacker2000
ID: 20429248
Did that work for you?
0
 

Author Comment

by:brianharrell
ID: 20429512
still waiting to test on a large email batch
0
 

Author Comment

by:brianharrell
ID: 20437487
No that did not work.

In my script I have this near the start of the script (not in the email loop part)

ob_implicit_flush();

ob_flush(); // added 12-05-2007
flush(); // added 11-15-2007 BH

My email script loop is below:

     $row = mysql_fetch_array($result); // get the data!
     if ( $row != "" ) {  
      // GOT vendor info!
        echo "Emailing Vendor ID ".$v_id." ... success!<br>";
        ob_flush(); // added 12-05-2007
        flush(); // added 11-15-2007 BH
    // wait for 100 milliseconds (1 second = 1000000)
    usleep(100000);
    mail($vendor_email_address_here, $subject, $vendor_message , $extra_stuff_here_from_reply_address);
    }


Above always prints ALL of them to the screen AFTER it completes the loop.




0
 

Author Comment

by:brianharrell
ID: 20437621
Also, when a large query is running like this, ALL other queries to that database from browsers seem to STALL until this query finishes emailing the list of 1000 vendors.   Isn't MYSQL capable of more than one query at a time from the same browser (Firefox, separate tab)?
0
 

Author Comment

by:brianharrell
ID: 20437628
Tested that issue in IE and it doesn't seem to care how many client files are opened at once while the query is running, must be a FIREFOX issue.   Didn't mean to mix issues here but there may be a shred of relevance to the current issue.  No need to work on the FFox or IE issue in this thread any more.  Back to why the output is not getting to the screen during the emailing...
0
 

Author Comment

by:brianharrell
ID: 20576673
It is erratic and sometimes firefox does them one by one to the screen and other times it waits until 300-500 are to print and others only until the browser finishes the script.
Don't know how to fix this one.
I think it is a browser issue.
0
 

Author Closing Comment

by:brianharrell
ID: 31413021
Seems to work in IE now.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question