query($sql); echo "records returned: " . count($result) . PHP_EOL; } catch (Exception $e) { echo $e->getMessage() . PHP_EOL; echo $e->getTraceAsString() . PHP_EOL; } } function getDatabase() { return new PDO('mysql:host=localhost;dbname=mysql', 'user', 'pass', Array( PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION )); } echo "before fork" . PHP_EOL; // this is an idle database connection that belongs to the parent and is not used by the child and should survive the fork $idledb = getDatabase(); doQuery($idledb); $db = getDataBase(); $pid = pcntl_fork(); if ($pid == -1) { die('could not fork'); } elseif ($pid) { // sleep for 1 sec, time for the child query sleep(1); echo "parent" . PHP_EOL; // this reconnect should not be necessary, but otherwise doQuery() will fail $db = getDatabase(); doQuery($db); } else { echo "child" . PHP_EOL; // this should initiate a new connection object, independant of the parent one $db = getDatabase(); doQuery($db); exit(); } echo "after fork" . PHP_EOL; // this one fails since the database connection has been closed by the child's exit doQuery($idledb); echo "end" . PHP_EOL;