2016-03-01 43 views
1

在試圖插入的記錄數組,我得到錯誤: 1. MySQL服務器已消失 2.錯誤讀取結果集的頭PHP庫MySQLi插入查詢錯誤 - 服務器關閉了

以下是詳細:

// DB2 
$host1 = 'localhost'; 
$user1 = 'root'; 
$pass1 = ''; 
$db1 = 'my_db'; 
$conn1 = mysqli_connect($host1, $user1, $pass1, $db1); 

.... 
... 
... 
... 

//echo '<pre>'; print_r($countryArr); die; 

$ countryArr結果: enter image description here

$query = "INSERT INTO `cities` (`country_id`, `city`, `soft_delete`, `date_added`) VALUES " . implode(',', $countryArr); 
mysqli_query($conn1, $query); 

錯誤: enter image description here

+0

要插入多少條記錄? 'MySQL服務器已經消失'是因爲php失去了與mysql的連接 –

+0

@FakhruddinUjjainwala將要插入大約50000條記錄。 –

+2

在php.ini中更改設置max_execution_time = 0(或3600000) –

回答

1

步驟1:

set_time_limit(0); // this will remove the time limit if any. 

步驟2:

還要檢查平,以保持連接活躍,http://php.net/manual/en/mysqli.ping.php

步驟3

嘗試批量執行插入。即500條語句批次。

+0

批處理語句有效。謝謝 –

+0

@KunwarbirS。很高興幫助你。 :) –

0

你的查詢需要13+秒,而你的MySQL服務器設置爲殺人數目少於13

更改此行中你的MySQL配置文件(通常位於/ etc小秒後連接到執行/ mysql)

wait_timeout=15 //any reasonable amount of time more than 13 seconds for your example 

PS:您可能還需要更改允許的最大連接數以反映wait_time中的更改。要做到這一點,請在mysql配置文件中更改這2行。

max_connections=90 
max_user_connections=60 

根據您的服務器的活動,更改最大連接可能是必需的。例如,如果您的超時時間爲5,並且您的最大連接數爲30,並且假設所有查詢都需要5秒才能處理,那麼您的服務器一次只能提供6個連接。

現在,假設您將超時更改爲15,問題需要15,您的服務器將只能提供2個連接。

希望有幫助!