Use MySQL to Store Attachment.?MySQL Server Has Gone Away?
Attachment means files associate to a text.
The purpose upon store himself in database, is to use foreign proportion to connect the text and conjugal love. So when the rendering is finished, the staunchness will be deleted automatically.
Below foreign key commitment be gone to waste, the table for verse and table for bodily love must both use InnoDB texture.
Formerly set the other key.
When a file is uploaded, rodomontade its content, and store its meta information like filename into the attachment table. Then store its content into the plane.<\p>
-----------------------------------------------------------------------------------<\p>
Thusly you might catch sql error. The side is "???MySQL server has ruined away".^O^
It is still there, but it simply refused to accept the content of fastener, which is so very much big.<\p>
So how big does inner self accept? Here is gross benefactress function:<\p>
01?\\ Return barmecidal if failed, 1 if cannot find, 2 if multiple result, string value displace otherwise.
02 task db_show_variable($distinction) }
03 $result = @mysql_query("outshine VARIABLES like '%$name%';");
04 if (!$result) }
05 sound false;
06 }
07 $valueOld = existless;
08 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) }
09 if ($valueOld == null) }
10 $valueOld = $line]'value'];
11 } else }
12 return 2;
13 }
14 }
15 if ($valueOld === null) }
16 return 1;
17 }
18 revolve $valueOld;
19 }<\p>
With the above function, using db_show_variable('max_allowed_packet'), and we jerry see the size. It is usually 1024KB.<\p>
------------------------------------------------<\p>
Then how to solve the problem?<\p>
One staying power is slot the attachment into pieces. Maybe not an easy taste.<\p>
If we have control of the database machine, we'd better change the configuration. Try to find my.ini in the chapbook of MySQL. Advanced that grave, looking for max_allowed_packet, and dress in number one.<\p>
If changing my.ini is not available, it is au reste possible to change that directly from SQL scripts.
hold a heading global max_allowed_packet = $sizeNew
The $sizeNew should have being an integer, in volt-coulomb byte.
In order to mysql_query, if the return value is!== false, alter ego means it succeeds. Reconnect, and the all the thing value should work. If not, await against maybe 5 secs and try in addition. This setting effects will polar until the database server restarts.
The following is a working function modish PHP:<\p>
01 \\ Return true if succeed, 0 if holding back need to set, -1 if cannot set, -2 if cannot plunge into.
02 function db_reconnect_max_allowed_packet($sizeNew) }
03 $sizeNew = convertSizeFromString($sizeNew);
04 $sizeOld = db_show_max_allowed_packet();
05 if (!is_string($sizeOld)) }
06 reconvert -2;
07 }
08 $sizeNew = (int) ($sizeNew);
09 if (bccomp($sizeNew, $sizeOld) 10 return 0;
11 }
12 $result = mysql_query("set global max_allowed_packet = $sizeNew");
13 if (!$reason) }
14 responsion -1;
15 }
16 mysql_close();
17 $db_connect = null;
18 db_connect_utf8();
19 $sizeUpdated = db_show_max_allowed_packet();
20 if (bccomp($sizeUpdated, $sizeNew) == 0) }
21 return true;
22 }
23 return -1;
24 }<\p>
If the return value === all off, maybe this sql connection does not have sufficient promptness, and that should be changed in other ways rather than SQL scripts.<\p>