2014-04-08 32 views
3

我不知道爲什麼我遇到這個錯誤。發送STMT_PREPARE包時發生錯誤。 PID = 2

我有以下腳本:

 
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 ([email protected], source, www.url.com, https://www.facebook.com/url, some_username, http://instagram.com/url, 501)) 

總是在休息之前我得到29個查詢的執行,現在34

我想更好地理解這個錯誤:它可能是單個條目打破它,但即使我發佈的數據是亂碼,實際的數據對我來說。

我已經試過:

+0

如何與你的MySQL配置中的'max_allowed_pa​​cket'玩?這是一個警告權利? – majidarif

+0

@majimboo,我忘了提及我在共享主機...並從這個問題http://stackoverflow.com/questions/5688403/how-to-check-and-set-max-allowed-packet-mysql - 變量似乎我不能改變這個值,但我會試一試 – clod986

+0

插入查詢看起來像缺少單引號字符串值你可能需要檢查。 –

回答

1

我已經通過減少傳遞到foreach循環的項目解決了這個問題。

 
$all_brands = Brand::all(); 
$padding = 0; 
$batch  = 100; 

while($all_brands->count() > $padding){ 
    $brands = Brand::orderBy('id', 'asc')->skip($padding)->take($batch)->get(); 
    foreach($brands as $brand){ 
     .... 
     .... 
     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 
        )); 
    } 
    $padding = $padding + $batch; 
} 
0

我有同樣的問題,前幾天,所以對這個問題最簡單的辦法是讓.CNF配置文件的一些修改數據庫(MySQL的在我的情況)。需要更改的變量是wait_timeout。默認情況下,它是 ,但如果你增加它,那麼你不會得到這個錯誤。在我的情況下,我已將它設置爲10分鐘,現在我的腳本像魅力一樣工作。

發生了什麼事情是腳本會初始化,然後等待,直到我所有的API調用完成之後纔開始解析它們中的每一個。因此,直到它需要做數據庫查詢時,它已經失去了連接。希望能幫助到你!

+0

wait_timeout的默認值是8小時。 –

2

的真正原因和問題的解決方案可以在我的博客文章中找到:

Laravel 4 and "Error while sending STMT_PREPARE packet"

這是不可能來形容這裏是全部的答案,但是,長話短說,有在Laravel一個bug 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', 
    ]); 
} 
+0

已經有一段時間了,因爲我得到這個錯誤...我沒有時間來測試你的答案,但如果你設法得到一些upvotes我會接受這個答案 – clod986

+0

沒問題。只是想在別人面臨這個問題時做出貢獻。 –

相關問題