php - Error while sending STMT_PREPARE packet. PID=20615 -
edit: adding db::connection()->reconnect(); send method after retrieving emails session, alleviates issue. appears db getting disconnected or there many connections or something.
i believe have issue here mysql disconnecting on production server. server matches local dev setup (using homestead 2) , ubuntu, mysql, nginx , php-fpm.
i using laravel 4.2 , have created functionality interface mailchimp campaign, create static segment , add emails based on filtered criteria segment send email.
i running wherein() method on model retrieve collection of contacts matched array of emails, 2600 emails passed wherein() value.
on production server, kept receiving error (works flawlessly on local dev setup):
error while sending stmt_prepare packet. pid=20615
so, during research discovered related max_allowed_packet in mysql config of server, modified my.cnf , increased value 256m (on local dev machine value 16m , sufficient). had no effect on error.
i decided go ahead , rather use wherein(), test build new collection adding each contact 1 one unfortunately still receiving same error, when using 1 email. query failing in instance:
select * `contacts` `contacts`.`deleted_at` null , `email` = joebloggs@example.com limit 1
so points sort of misconfiguration on production server, though have made sure both local environment , production mysql configs match exactly.
so, in summary, local environment works fine , executes code no issues production server returns packet error.
i'm @ bit of loss on how proceed. can @ all, please?
here code using retrieve records:
// send() method filter results based on set of search fields, should rename filter public function send($id) { $campaign = $this->getsavedcampaign($id); $haveattended = explode(',', input::get('have_attended')); $havenotattended = explode(',', input::get('have_not_attended')); $account = explode(',', input::get('account')); $companyname = explode(',', input::get('company_name')); $industrytype = explode(',', input::get('industry_type')); $accountmanager = explode(',', input::get('account_manager')); $jobtitle = explode(',', input::get('job_title')); $contactemail = explode(',', input::get('contact_email')); // check if there has been post request , if filtered contact emails , add them session if (request::method() == 'post') { // retrieve array of contact emails based on filters supplied $contactemails = $this->contact->getcontactemails($haveattended, $havenotattended, $account, $companyname, $industrytype, $accountmanager, $jobtitle, $contactemail)->lists('email'); // create new mailchimp static segment , return id $segmentid = $this->createnewsegment(mailchimpwrapper::lists()->staticsegments('123456789'), $id); // add emails array generated above static segment in mailchimp $this->addcontactemailstosegment($contactemails, $segmentid); // retrieve emails matched subscriber in mailchimp added static segment $emails = $this->getsegmentemails(config::get('mailchimp::apikey'), '123456789', 'subscribed', $segmentid); // put emails array , segment id in session session::put('emails', $emails); session::put('segmentid', $segmentid); } // check if session contains array of emails , if retrieve them , pull eloquent collection of contacts match emails stored in session if (session::get('emails')) { $emails = session::get('emails'); // edit: if add db::connection()->reconnect(); here allieviates issue $contacts = $this->contact->getcontactemails($haveattended, $havenotattended, $account, $companyname, $industrytype, $accountmanager, $jobtitle, $contactemail, $emails)->paginate(10)->appends([ 'have_attended' => input::get('have_attended'), 'have_not_attended' => input::get('have_not_attended'), 'account' => input::get('account'), 'industry_type' => input::get('industry_type'), 'account_manager' => input::get('account_manager'), 'job_title' => input::get('job_title'), 'contact_email' => input::get('contact_email') ]); } $this->layout->content = view::make('admin.newsletters.send', compact('campaign', 'contacts'))->witherrors($this->errors); }
this how retrieve mailchimp campaign:
// getsavedcampaign() method retrieves mailchimp campaign attaching filtered segment public function getsavedcampaign($id) { $campaign = (object) mailchimpwrapper::campaigns()->getlist(['campaign_id' => $id], 0, 1)['data'][0]; $campaign->create_time = new carbon($campaign->create_time); $campaign->send_time = new carbon($campaign->send_time); $campaign->content_updated_time = new carbon($campaign->content_updated_time); return $campaign; }
this how retrieve contacts/contact emails contacts table based on filtered results:
public function scopegetcontactemails($query, $haveattended, $havenotattended, $account, $companyname, $industrytype, $accountmanager, $jobtitle, $contactemail, $emails = null) { // if emails session variable exists (is not null) run wherein query on contacts table retrieve contacts match email in array, otherwise run filters if ( ! is_null($emails)) { $query->wherein('email', $emails); } else { $query->orderby('email', 'asc') ->where('contactable', 0) ->where('opt_out', 0) ->where('email', '!=', db::raw("concat('', email * 1)")); if ( ! empty($companyname[0])) { $query->wherehas('account', function($query) use ($companyname) { $query->where('company_name', 'like', "%$companyname[0]%"); }); } if ( ! empty($account[0])) { $query->wherehas('account', function($query) use ($account) { $query->wherein('id', $account); }); } if ( ! empty($accountmanager[0])) { $query->wherehas('account', function($query) use ($accountmanager) { $query->wherehas('user', function($query) use ($accountmanager) { $query->wherein('id', $accountmanager); }); }); } if ( ! empty($industrytype[0])) { $query->wherehas('account', function($query) use ($industrytype) { $query->wherehas('industrytype', function($query) use ($industrytype) { $query->wherein('id', $industrytype); }); }); } if ( ! empty($haveattended[0]) or ! empty($havenotattended[0])) { $query->join('delegates', 'contacts.id', '=', 'delegates.contact_id') ->join('delegate_event', 'delegates.id', '=', 'delegate_event.delegate_id') ->join('events', 'delegate_event.event_id', '=', 'events.id') ->join('courses', 'events.course_id', '=', 'courses.id'); } if ( ! empty($haveattended[0])) { $query->wherein('courses.id', $haveattended); } if ( ! empty($havenotattended[0])) { $query->wherenotin('courses.id', $havenotattended); } if ( ! empty($jobtitle[0])) { $query->wherein('contacts.job_title_id', $jobtitle); } if ( ! empty($contactemail[0])) { $query->wherein('contacts.id', $contactemail); } } }
this how create new mailchimp segment:
public function createnewsegment($segments, $campaignid) { foreach ($segments $key => $segment) { if ($segment['name'] == 'cream-' . $campaignid) { mailchimpwrapper::lists()->staticsegmentdel('123456789', $segment['id']); } } $segment = mailchimpwrapper::lists()->staticsegmentadd('123456789', 'cream-' . $campaignid); return $segment['id']; }
this how add emails retrieved created segment:
public function addcontactemailstosegment($contactemails, $segmentid) { $listemails = $this->generatelistemails(config::get('mailchimp::apikey'), '123456789', 'subscribed'); $emails = $this->buildsegmentemails($contactemails, $listemails); if ($emails) { $emailschunk = array_chunk($emails, 1000); foreach ($emailschunk $emails) { mailchimpwrapper::lists()->staticsegmentmembersadd('123456789', $segmentid, $emails); } } }
this function retrieves emails/contacts have matched subscribers in mailchimp , have been added segment:
public function getsegmentemails($apikey, $listid, $status, $segmentid) { $conditions = '&segment[saved_segment_id]=' . $segmentid; return $this->generatelistemails($apikey, $listid, $status, $conditions); }
this how list emails pulled mailchimp:
public function generatelistemails($apikey, $listid, $status, $conditions = null) { $url = 'http://us5.api.mailchimp.com/export/1.0/list?apikey=' . $apikey . '&id=' . $listid . '&status=' . $status; if ( ! is_null($conditions)) { $url .= '&segement[match]=all' . $conditions; } $handle = @fopen($url, 'r'); $chunk_size = 4096; $i = 0; $header = []; while ( ! feof($handle)) { $buffer = fgets($handle, $chunk_size); if (trim($buffer) != '') { $obj = json_decode($buffer); $listemails[$obj[0]] = $obj[0]; $i++; } } fclose($handle); unset($listemails['email address']); return $listemails; }
Comments
Post a Comment