I am placing multiple rows inside a table, and that i understand this message:

MySQL server went away

My Query:

INSERT INTO table
 (a,b,c,d,e,f,g,h,i,j,k) 
     VALUES(1,2,3,4,5,6,7,8,9,10,11),(1,2,3,4,5,6,7,8,9,10,11), ...
        ON DUPLICATE KEY UPDATE
          c=VALUES(c),
          d=VALUES(d),
          e=VALUES(e),
          f=VALUES(f),
          g=VALUES(g),
          h=VALUES(h),
          i=VALUES(i),
          j=VALUES(j)

Could it be because I stuffed a lot of values in the single query? (You will find like 5000 pairs of values from the array that we implode with ,).

If because of this , - then must i place each row 1 by 1? Could it be reduced than placing them all at one time?


The PHP code:

foreach($data as &$entry)
 $entry = "('".implode("','", array(
   $entry->ID,
   addslashes($entry->field_1),
   addslashes($entry->field_2),
   ...

 ))."')";

$data = implode(',', $data);   

$query = "... VALUES{$data} ON ..."

$data is really a variety of STD type objects...


edit again :)

And So I attempted splitting my $data into more compact arrays of 100 elements each:

$data_chunks = array_chunk($data, 100);
foreach($data_chunks as $data_chunk)
  insert_into_db($data_chunk);

and delay pills work, I do not have that error any longer...

To ensure that means the problem was the lengthy query string...

Now I am much more confused:

  • It is possible to length limit from the query, or possibly PHP arguments generally?
  • Can there be any distinction between placing row by row than placing multiple rows? Could it be well worth the array_chunk() ?

maybe your totally taking too lengthy to accomplish, mysql occasions out and shuts the bond. You are able to affect the system variables to hold back longer.

http://dev.mysql.com/doc/refman/5.0/en/gone-away.html

I believe your condition is by using *max_permitted_packet*, even though error appears to suggest in various direction. Try doing as recommended here: http://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html

Or, prior to making any changes to mysql configuration, simply strlen() your query and discover how lengthy(in bytes) it really is.