Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 发送STMT\U准备数据包时出错。PID=20615_Php_Mysql_Laravel_Laravel 4_Mailchimp - Fatal编程技术网

Php 发送STMT\U准备数据包时出错。PID=20615

Php 发送STMT\U准备数据包时出错。PID=20615,php,mysql,laravel,laravel-4,mailchimp,Php,Mysql,Laravel,Laravel 4,Mailchimp,编辑:添加数据库::连接()->重新连接();在从会话中检索电子邮件后使用send方法,可以缓解此问题。因此,数据库似乎正在断开连接,或者连接太多或其他情况 我认为在生产服务器上断开MySQL连接有问题。该服务器与我的本地开发设置(使用Homestead2)相匹配,是Ubuntu、MySQL、Nginx和PHP-FPM 我使用的是Laravel 4.2,我创建了与Mailchimp活动交互的功能,创建了一个静态段,并根据过滤标准向该段添加电子邮件以发送电子邮件 我最初在一个模型上运行where(

编辑:添加数据库::连接()->重新连接();在从会话中检索电子邮件后使用send方法,可以缓解此问题。因此,数据库似乎正在断开连接,或者连接太多或其他情况

我认为在生产服务器上断开MySQL连接有问题。该服务器与我的本地开发设置(使用Homestead2)相匹配,是Ubuntu、MySQL、Nginx和PHP-FPM

我使用的是Laravel 4.2,我创建了与Mailchimp活动交互的功能,创建了一个静态段,并根据过滤标准向该段添加电子邮件以发送电子邮件

我最初在一个模型上运行where()方法来检索匹配一系列电子邮件的联系人集合,大约有2600封电子邮件作为值传递给where()

在我的生产服务器上,我不断收到错误(在本地开发人员设置中工作正常):

因此,在我的研究中,我发现它可能与服务器的MySQL配置中的max_allowed_数据包有关,因此我修改了my.cnf并将该值增加到256M(在我的本地开发机器上,该值为16M,已经足够了)。这对错误没有影响

我决定继续,而不是使用where(),作为一个测试构建一个新的集合,逐个添加每个联系人,但不幸的是,即使使用1封电子邮件,我仍然收到相同的错误。这是在此实例中失败的查询:

select * from `contacts` where `contacts`.`deleted_at` is null and `email` = joebloggs@example.com limit 1
因此,这显然表明我的生产服务器存在某种配置错误,尽管我已经确保本地环境和生产MySQL配置完全匹配

总之,我的本地环境运行良好,执行代码时没有问题,但生产服务器返回数据包错误

我现在有点不知道该怎么做。有人能帮忙吗

以下是我用来检索记录的代码:

// send() method to filter results based on a set of search fields, probably should rename this to 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 a POST request and if so get the filtered contact emails and add them to the session
    if (Request::method() == 'POST')
    {
        // Retrieve an array of contact emails based on the filters supplied
        $contactEmails = $this->contact->getContactEmails($haveAttended, $haveNotAttended, $account, $companyName, $industryType, $accountManager, $jobTitle, $contactEmail)->lists('email');

        // Create a new Mailchimp static segment and return its ID
        $segmentId = $this->createNewSegment(MailchimpWrapper::lists()->staticSegments('123456789'), $id);

        // Add the emails array generated above to the static segment in Mailchimp
        $this->addContactEmailsToSegment($contactEmails, $segmentId);

        // Retrieve all the emails that matched a subscriber in Mailchimp that were added to the static segment
        $emails = $this->getSegmentEmails(Config::get('mailchimp::apikey'), '123456789', 'subscribed', $segmentId);

        // Put the emails array and the segment id in to the session
        Session::put('emails', $emails);
        Session::put('segmentId', $segmentId);
    }

    // Check if the session contains an array of emails and if so retrieve them and pull back an eloquent collection of contacts that match the emails stored in the session
    if (Session::get('emails'))
    {
        $emails = Session::get('emails');

        // EDIT: If I add DB::connection()->reconnect(); here this allieviates the 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);
}
这是我如何检索Mailchimp活动的:

// getSavedCampaign() method which retrieves the Mailchimp campaign we will be attaching the filtered segment to

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;
}
这是我根据筛选结果从联系人表中检索联系人/联系人电子邮件的方式:

public function scopeGetContactEmails($query, $haveAttended, $haveNotAttended, $account, $companyName, $industryType, $accountManager, $jobTitle, $contactEmail, $emails = null)
{
    // If the emails session variable exists (is not null) simply run a whereIn query on the contacts table to retrieve all contacts that match an email in the array, otherwise run the 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);
        }
    }
}
这就是我如何创建新的Mailchimp段的方法:

public function createNewSegment($segments, $campaignId)
{
    foreach ($segments as $key => $segment)
    {
        if ($segment['name'] == 'CREAM-' . $campaignId)
        {
            MailchimpWrapper::lists()->staticSegmentDel('123456789', $segment['id']);
        }
    }

    $segment = MailchimpWrapper::lists()->staticSegmentAdd('123456789', 'CREAM-' . $campaignId);

    return $segment['id'];
}
以下是我如何将检索到的电子邮件添加到创建的邮件段:

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 as $emails)
        {
            MailchimpWrapper::lists()->staticSegmentMembersAdd('123456789', $segmentId, $emails);
        }
    }
}
此功能用于检索Mailchimp中已匹配订户并已添加到段中的所有电子邮件/联系人:

public function getSegmentEmails($apiKey, $listId, $status, $segmentId)
{
    $conditions = '&segment[saved_segment_id]=' . $segmentId;

    return $this->generateListEmails($apiKey, $listId, $status, $conditions);
}
以下是如何从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;
}

你的服务器有多少内存?生产服务器上有4GB内存我也有同样的问题!我想知道:“在从会话检索电子邮件后将DB::connection()->reconnect();添加到send方法”感觉不是一个优雅的解决方案,因为它似乎仍然需要解决某个根本原因。你有没有想过?谢谢
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;
}