Pcntl_fork causes MySQL server has gone away solution

Source: Internet
Author: User

The MySQL server has gone away error pcntl_fork before the database is connected. The reason is that the child process inherits the database connection from the main process, and when MySQL returns the data, these sub-processes can read the data through the connection, causing the data to be garbled.

The operation of the database is the place to operate the database, to solve this problem, in $pid = pcntl_fork(); Before clearing out the MySQL connection before

Interpretation of official documents and solutions

http://php.net/manual/en/function.pcntl-fork.php#70721

So I try, gee, or not AH ~ ~ ~ again look at the code, found something wrong ~ ~ to   $pid  = pcntl_fork ()   before the MySQL connection is cleared, why the above code is $db  = mysql_connect ( $server ,  $username ,  $password

So, I am in $pid = pcntl_fork(), before change to Disconnet ~

                        $model _mod=Newmodel_base (); $model _mod->disconnect (); $pid=pcntl_fork (); //both the parent and child processes execute the following code                        if($pid= =-1) {                            //error Handling: Returns -1 when creating a child process failure.                             die(' could not fork '); } Else if($pid) {                            $model _mod->connect (); //The parent process gets the child process number, so this is the logic that the parent process executesPcntl_wait ($status);//wait for the child process to break to prevent the child process from becoming a zombie process. }Else {                                                        //the child process gets a $pid of 0, so here is the logic that the child process executes. }

Finally successfully solve the problem ~ ~

Pcntl_fork causes MySQL server has gone away solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.