• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
fork: 'MySQL server has gone away' - How not to lose connection after the DB proceess

I installed fork on my Ubuntu Server (using PHP-Apache-Codeigniter). 

I have this code: 

    public function add_keyword() {
        $keyword_p = $this->input->post('key_word');
        $prod      = $this->input->post('prod_name');
        $prod      = $this->kas_model->search_prod_name($prod);
        $prod      = $prod[0]->prod_id;
        $country   = $this->input->post('key_country');
        $keyword = explode(", ", $keyword_p);
        $keyword_count = count($keyword);
        echo "the keyword count: $keyword_count";
        // Create   fork
        $pid = pcntl_fork();
            for ($i=0; $i < $keyword_count ; $i++) { 
                // Inserts the inputs to the "keywords" table
                $this->kas_model->insert_keyword($keyword[$i], $prod, $country);
                // Gets relevant IDs for the inserted prod and keyword
                $last_inserted_key = $this->kas_model->get_last_rec('keywords');
                $keyword_id        = $last_inserted_key[0]->key_id;
                $prod_id           = $last_inserted_key[0]->key_prod;
                $prod_id_query     = $this->kas_model->get_prod_row_by_id($prod_id);
                $prod_id_a  = $prod_id_query[0]->prod_a_id;
                $prod_id_b  = $prod_id_query[0]->prod_b_id; 
                // Run the keyword query (on API) for today on each one of the keys and insert to DB aslong that the ID isn't 0.  
                if ( ($prod_id_a != 0) || ( !empty($prod_id_a) ) ) {
                    $a_tdr = $this->get_var1_a_by_id_and_kw( $prod_id_a, $keyword[$i], $country);
                } else {
                    $a_tdr['var1'] = 0;
                    $a_tdr['var2'] = 0;
                    $a_tdr['var3'] = 0;
                if ( ($prod_id_b != 0) || ( !empty($prod_id_b) ) ) {
                    $b_tdr = $this->get_var1_b_by_id_and_kw($prod_id_b, $keyword[$i], $country);
                } else {
                    $b_tdr['var1'] = 0;
                    $b_tdr['var2'] = 0;
                    $b_tdr['var3'] = 0;     
                $this->kas_model->insert_new_key_to_db($keyword_id, $a_tdr['var1'], $b_tdr['var1'], $a_tdr['var2'], $b_tdr['var2'], $a_tdr['var3'], $b_tdr['var3']);

What the function does: This function gets 1 or more keyword/s, a country var, and a product ID, and runs a query on an external slow API getting variables (runs other functions from within that same controller), and adds them to the database.

Solution: When running this function, and if I insert a lot of keywords, the page loads, and loads, and loads, for a long time, until it's done, and only then - I can continue browsing my website. So I was told to fork it since it's just sending a request to process it in the background, so whenever clicking the submit button, I get redirected to "main/kas".

Currently: I get the my MySQL connection disconnected with this error: 

> MySQL server has gone away

But with inserting 1 var from all the process. 

What I trird till now: As said in this comment: 

I have to reconnect to my DB in the parent, and that should solve the problem. 
I have found this post from 2009 too: http://stackoverflow.com/questions/18841...n-php-site 

 So I continued my "if" with this: 

    // parent    
    } else if ( $pid ) {

I tried adding this in the end of the child too (frustration) and in all sorts of variations (regular `$link = mysqli_connect("", "my_user", "my_password", "my_db");`) - no success. 

Even tried using only `$this->load->database();` or only `$this->db->reconnect();`. 

What is the solution for this, this is so frustrating :'(

Is this a bug?

Messages In This Thread
fork: 'MySQL server has gone away' - How not to lose connection after the DB proceess - by hAtul - 04-21-2016, 01:48 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.