Threading in php

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
achinthpaul
Forum Newbie
Posts: 4
Joined: Fri Mar 13, 2009 5:01 am

Threading in php

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Threading in php

Post 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]
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Threading in php

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: Threading in php

Post by Jenk »

There is no multi-threading in PHP.
vorthtoura
Forum Newbie
Posts: 2
Joined: Wed Mar 18, 2009 1:48 am

Re: Threading in php

Post by vorthtoura »

The best idea in your case, standalone script which start up from cronjobs
Post Reply