php - 发送 STMT_PREPARE 数据包时出错。 PID=2

标签 php mysql laravel laravel-4 eloquent

我不知道为什么我会遇到这个错误。

我有以下脚本:

foreach($brands as $brand){ // about 600items for this loop
    ....
    ....
    DB::table('mailing_list')->insert(array(
                        'email'     => $email,
                        'source'    => $source,
                        'source_url'=> $brand->external_url,
                        'facebook_url'  => $facebook_url,
                        'instagram_id'  => $instagram_username,
                        'instagram_url' => $instagram_url,
                        'instagram_followers'   => $instagram_followers
                    ));
}

总是在同一个项目处中断

Error while sending STMT_PREPARE packet. PID=2 (SQL: insert into `mailing_list` (`email`, `source`, `source_url`, `facebook_url`, `instagram_id`, `instagram_url`, `instagram_followers`) values (some@email.com, source, www.url.com, https://www.facebook.com/url, some_username, http://instagram.com/url, 501))

之前我执行了 29 个查询,现在是 34 个。

我想更好地理解这个错误:它可能是单个条目破坏了它,但即使我发布的数据是乱码,实际的数据对我来说看起来还不错。

我尝试了什么:

set_time_limit(3600);
DB::connection()->disableQueryLog();
DB::reconnect(Config::get('database.default')); // this one after each foreach loop

最佳答案

问题的真正原因和解决方案可以在我的博文中找到:

Laravel 4 and "Error while sending STMT_PREPARE packet"

不可能在这里描述完整的答案,但长话短说,Laravel 4 的 \Illuminate\Database\MySqlConnection 类中存在错误。具体来说,在其 causedByLostConnection 方法中,如下所示:

/**
 * Determine if the given exception was caused by a lost connection.
 *
 * @param  \Illuminate\Database\QueryException
 * @return bool
 */
protected function causedByLostConnection(QueryException $e)
{
    return str_contains($e->getPrevious()->getMessage(), 'server has gone away');
}

不幸的是,“服务器已经消失”并不是唯一表明与 MySQL 的连接丢失的消息。

Laravel 5 中的相同方法检查 6 条额外的消息,这解决了问题:

/**
* Determine if the given exception was caused by a lost connection.
*
* @param  \Exception  $e
* @return bool
*/
protected function causedByLostConnection(Exception $e)
{
    $message = $e->getMessage();

    return Str::contains($message, [
        'server has gone away',
        'no connection to the server',
        'Lost connection',
        'is dead or not enabled',
        'Error while sending',
        'decryption failed or bad record mac',
        'SSL connection has been closed unexpectedly',
    ]);
}

关于php - 发送 STMT_PREPARE 数据包时出错。 PID=2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22932723/

相关文章:

laravel - 运行 make :request on laravel 5 时出错

php - 通过 PHP header 或 HTML 元 http-equiv 发送文档信息?

php - 如何使用自定义社区/城市/州在 drupal 或 elgg 上注册?

php - 通过php用mysql比较两个表

php - 在 laravel 5.5 中创建自定义外部类

php - 如何使用 DB::raw 查询输出 MySQL 版本

php - 如何将不同的(外部)WordPress 数据库服务器连接到另一个 WordPress 服务器?

javascript - 为什么参数读取为true?

mysql - 如何在mysql中自定义 `show processlist`?

php - Symfony2 : Left Join producing wrong query