Page 1 of 1

Wait for value in database before continuing script

Posted: Wed May 03, 2006 9:46 am
by MarkAshley
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Is it possible to make a script check for a value in a database every x seconds, and then proceed with the script once it has been found? I have tried putting this at the end of the script:

Code: Select all

Echo "Page content here";
$num = 0;
while ( $num == 0 ) {
	sleep(5);
	$query="select * from messages where recipient='$username'";
	$result=mysql_query($query);
	$num=mysql_numrows($result);
}
header("Location: messages.php");
I thought this would display the contents of the page until the query in the while loop returned a result, then the while loop would break and the header() command would be executed, but the page remained blank until the while loop completed, at which point the header() command executed. The problem is that none of the contents of the page is displayed.

Is it possible to do what I am asking?

Thanks
Mark


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed May 03, 2006 10:02 am
by feyd
look into flush(), however this is not a guaranteed output.

Posted: Wed May 03, 2006 10:21 am
by MarkAshley
Hi feyd, thanks very much for your quick response! I put the flush() command before the start of the while loop, and it did indeed "flush" all the output up to that point in to the browser. I then simulated the user receiving a message by inserting a record in to the messages table, and the script continued!

The problem is that after 30 seconds the script times out because the max execution time of 30 seconds is exceeded. I tried increasing this in the php config file then stopping and restarting Apache, but it still times out after 30 seconds. Is there somewhere else I need to change this? Or can I not increase it to more than 30 seconds?

Thanks
Mark

Posted: Wed May 03, 2006 10:23 am
by feyd
You may have altered the wrong php.ini, but you can set the time limit on a page-by-page basis too with set_time_limit()

Posted: Thu May 04, 2006 8:04 am
by MarkAshley
Wicked - thanks :D

Mark