Page 1 of 1

Threading in php

Posted: Mon Mar 16, 2009 5:22 am
by achinthpaul
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.

:banghead:

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

Re: Threading in php

Posted: Mon Mar 16, 2009 5:29 am
by Benjamin
Forum Rules 1-1.1-1 wrote: Select the correct board for your query. Take some time to read the guidelines in the sticky topic.
Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]

Re: Threading in php

Posted: Mon Mar 16, 2009 6:34 am
by Paul Arnold
I think what you're looking for is a CRON job mate.

Basically a script that runs on the server at defined intervals.

Re: Threading in php

Posted: Mon Mar 16, 2009 7:10 am
by Jenk
There is no multi-threading in PHP.

Re: Threading in php

Posted: Wed Mar 18, 2009 2:38 am
by vorthtoura
The best idea in your case, standalone script which start up from cronjobs