Reason for the error

The parent and child processes all share the same database connection. When the first child process exits it will disconnect from the database, which means the same connection all processes are using will be disconnected, causing any further queries to fail.

The solution

The solution is to disconnect from the database before forking the sub processes and then establish a new connection in each process. The fourth parameter also should be passed to the mysql_connect function as "true" to ensure a new link is established; the default is to share an existing connection is the login details are the same.

//fork the process to work in a daemonized environment
file_put_contents($log, 'Status: starting up
$pid = pcntl_fork();
if($pid == -1){
  file_put_contents($log, 'Error: could not daemonize process
  return 1; //error
else if ($pid)
  return 0;
  $connect = sql_connect($db_host, $db_user, $db_pass, $db_name);
  //the main process
    $result = SendEmailValidation();
    file_put_contents($log, $result, FILE_APPEND);
  }//end while
}//end if

저작자 표시

+ Recent posts

티스토리 툴바