Threading in php
Posted: Mon Mar 16, 2009 5:22 am
Hi All,
I am new to php.What I want to know is there any way to execute threads using php.
I am developing a mailing system.So every 10 minutes I need to check whether any new
mails have come.This program should run in background continuously.I already tried two methods like (1) I put the code in a infinite while loop and gave sleep(1) code :
<?php
function func(){
include "mysql_connection.php";
$bc = true;
$sql ="select communication_center_id,message,message_timestamp from communication_center where to_person_id ='17' and message_status='UNREAD'";
$rs3 = mysql_query($sql, $con);
while($row = mysql_fetch_array($rs3))
{
echo "Id:". $row['communication_center_id'];
echo "\n";
echo "Msg:". $row['message'];
echo "\n";
echo "Time:".$row['message_timestamp'] ;
echo "\n";
}
while($bc = true){
$sql2 = "SELECT COUNT(*) FROM " .communication_center." where to_person_id ='$edyalay_user_id' and message_status='UNREAD'";
if ($rs2 = mysql_query($sql2, $con)) {
print(mysql_num_rows($rs2) . " new mail.\n");
}
echo "current time :".time();
echo "\n";
sleep(1);
func1();
echo "current time after sleep ".time();
echo "\n";
$sql3 ="insert into communication_center values (5,now(),3,'PLAIN',17,'Hi.! How are you 1?','','UNREAD',1)";
}
mysql_close($con);
}
// wait for 2 seconds
function func1(){
$bc = true;
//while ($bc = true) {
echo "Hi \n";
//sleep(1);
//echo "2";
//}
}
func();
?>
But now what is happening is it will be in a hanged state forever.
(2) second method I tried is to use recursion .But that also showing error.
I am really trying hard but not getting any help.
Can any one of you help me.It is getting disperate.
Thanks in advance,
Paul
I am new to php.What I want to know is there any way to execute threads using php.
I am developing a mailing system.So every 10 minutes I need to check whether any new
mails have come.This program should run in background continuously.I already tried two methods like (1) I put the code in a infinite while loop and gave sleep(1) code :
<?php
function func(){
include "mysql_connection.php";
$bc = true;
$sql ="select communication_center_id,message,message_timestamp from communication_center where to_person_id ='17' and message_status='UNREAD'";
$rs3 = mysql_query($sql, $con);
while($row = mysql_fetch_array($rs3))
{
echo "Id:". $row['communication_center_id'];
echo "\n";
echo "Msg:". $row['message'];
echo "\n";
echo "Time:".$row['message_timestamp'] ;
echo "\n";
}
while($bc = true){
$sql2 = "SELECT COUNT(*) FROM " .communication_center." where to_person_id ='$edyalay_user_id' and message_status='UNREAD'";
if ($rs2 = mysql_query($sql2, $con)) {
print(mysql_num_rows($rs2) . " new mail.\n");
}
echo "current time :".time();
echo "\n";
sleep(1);
func1();
echo "current time after sleep ".time();
echo "\n";
$sql3 ="insert into communication_center values (5,now(),3,'PLAIN',17,'Hi.! How are you 1?','','UNREAD',1)";
}
mysql_close($con);
}
// wait for 2 seconds
function func1(){
$bc = true;
//while ($bc = true) {
echo "Hi \n";
//sleep(1);
//echo "2";
//}
}
func();
?>
But now what is happening is it will be in a hanged state forever.
(2) second method I tried is to use recursion .But that also showing error.
I am really trying hard but not getting any help.
Can any one of you help me.It is getting disperate.
Thanks in advance,
Paul