PHP forking and mysql database connection problem

Posted by user298819 on Stack Overflow See other posts from Stack Overflow or by user298819
Published on 2010-03-22T07:31:55Z Indexed on 2010/03/22 7:41 UTC
Read the original article Hit count: 507

Filed under:
|
|

I am now trying to do forking in php. I would like to do some query and update in child process.. the problem is that whenever a child process finish, it close the connection which makes the other queries fail. The following is my sample code!!

#!/usr/local/bin/php
<?php
set_time_limit(0); # forever program!
    $db = mysql_connect("server","user","pwd");
    mysql_select_db("schema",$db);
    $sql = "query";
    $res = mysql_query($sql,$db);
while($rows = mysql_fetch_array($res)) {
    $rv = pcntl_fork();
    if($rv == -1){
        echo "forking failed";
    }elseif($rv){
        echo "parent process $rv\n";
        $db = mysql_connect("192.168.8.112","zwmuser","zwmuser",true);
        mysql_select_db("schema",$db);
    }else{
        echo "child process $rv\n";
        $sql1 = "another query";
        $res1 = mysql_query($sql1,$db);
        while($messages = mysql_fetch_array($res1)) {
            $sql2 = "update query";
            mysql_query($sql2,$db);
        }
        exit(0);
                //it terminates both child process and mysql connection!
    }
}
?>

© Stack Overflow or respective owner

Related posts about php

Related posts about linux