<?xml version="1.0"?>
<rss version="2.0"><channel><title>Devplace</title><link>http://www.devplace.nl/blog</link><lastBuildDate>Sat, 26 Dec 09 10:54:52 +0100</lastBuildDate><generator>Habari 0.7-alpha http://habariproject.org/</generator><item><title>Storing large values in MySQL fields with PHP</title><link>http://www.devplace.nl/blog/storing-large-values-in-mysql-fields-with-php</link><description>Today a colleague at work ran into a problem when trying to store file contents into a MySQL database table. We are using this solution for quite some time now and it has always worked pretty good for documents and files up to 15 MB. The main reason for us to put files in the database is that it is easier to migrate a website to another server because the database contains all the data that we need. &#xD;
&#xD;
The problem appeared at a customers' webserver that is running Fedora. First we thought there must be something wrong with our PHP code. But as the code worked fine on our servers (Debian) we soon found out that we couldn't store more than 1 MB in our content field in the database. After a little more investigation the only solution to the problem seems to be raising the mysql system variable &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_allowed_packet"&gt;max_allowed_packet&lt;/a&gt; to a value that is bigger than the largest content that should go into a single field. &#xD;
&#xD;
For Debian the default max_allowed_packet size is 16 MB, but since the max_allowed_packet is not set for Fedora, MySQL defaults to 1 MB. We have no access to the MySQL server configuration so we had to solve problem this on the client side (our PHP code). An example of how this can be done is given here.&#xD;
&#xD;
&lt;pre class="brush: php"&gt;&#xD;
$fileName = '/file/name/of/uploaded/file.bin';&#xD;
$fileSize = filesize($fileName);&#xD;
&#xD;
// $db holds the (ADODB) database connection&#xD;
// Fetch current packet size&#xD;
$packetSize = (int) $db-&gt;GetOne('SELECT @@max_allowed_packet');&#xD;
if ($packetSize &amp;lt; $fileSize + 2048) {&#xD;
	$sql = sprintf('SET @@max_allowed_packet=%d', $fileSize + 2048);&#xD;
	$db-&gt;Execute($sql);&#xD;
}&#xD;
&#xD;
$db-&gt;Execute('INSERT INTO `file`(`filename`, `filesize`) VALUES(?, ?)', array($fileName, $fileSize));&#xD;
$fileId = $db-&gt;Insert_ID();&#xD;
&#xD;
// Here you can store the file contents in database&#xD;
$handle = fopen($fileName, 'r');&#xD;
while (($data = fread($handle, 524288)) &amp;&amp; strlen($data) &gt; 0) {&#xD;
	// Update record with chunks of 512K, until the file is completely in the database&#xD;
	// This prevents PHP from running out of memory&#xD;
	$sql = sprintf(&#xD;
		'UPDATE `file` &#xD;
		SET `contents` = CONCAT(`contents`, ?) &#xD;
		WHERE `id` = ?');&#xD;
	$this-&gt;db-&gt;Execute($sql, array($db-&gt;Quote($data), $fileId));&#xD;
}&#xD;
fclose($handle);&#xD;
&#xD;
// now set the  max_allowed_packet back to old value&#xD;
$sql = sprintf('SET @@max_allowed_packet=%d', $packetSize);&#xD;
$db-&gt;Execute($sql);&#xD;
&lt;/pre&gt;&#xD;
&#xD;
It is possible to store values up to 1 GB in a field in MySQL (as &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/server-system-variables.html#sysvar_max_allowed_packet"&gt;1 GB is currently the maximum allowed packet size&lt;/a&gt;). It is however not advisable to use the above method using &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat"&gt;CONCAT&lt;/a&gt; to put large files in the database, &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat"&gt;CONCAT&lt;/a&gt; gets very slow when the size of the blob gets bigger. If you want to put large files in the database you probably want to take a look at the &lt;a href="http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_load-file"&gt;LOAD_FILE&lt;/a&gt; function. This is however more complex if MySQL doesn't run on the same server as your PHP code is running on.&#xD;
&#xD;
As you can see in the above PHP code the following MySQL queries can be used to retrieve and set the max_allowed_packet variable for the current connection.&#xD;
&#xD;
&lt;pre class="brush: sql"&gt;&#xD;
-- Fetch maximum allowed packet size (in bytes)&#xD;
SELECT @@max_allowed_packet;&#xD;
&#xD;
-- Set maximum allowed packet size (in bytes)&#xD;
SET @@max_allowed_packet=33554432;&#xD;
&lt;/pre&gt;&#xD;
&#xD;
We use the following table definition to store the files (I have left out some unimportant fields). &#xD;
&#xD;
&lt;pre class="brush: sql"&gt;&#xD;
CREATE TABLE `file` (&#xD;
  `id` int(10) unsigned NOT NULL auto_increment,&#xD;
  `filename` varchar(255) NOT NULL,&#xD;
  `filesize` varchar(50) NOT NULL,&#xD;
  `contents` longblob NOT NULL,&#xD;
  PRIMARY KEY  (`id`)&#xD;
) ENGINE=InnoDB CHARSET=utf8&#xD;
&lt;/pre&gt;</description><pubDate>Tue, 29 Sep 09 22:55:00 +0200</pubDate><guid isPermaLink="false">tag:www.devplace.nl,2009:storing-large-values-in-mysql-fields-with-php/1254255396</guid></item><item><title>About me</title><link>http://www.devplace.nl/blog/about-me</link><description>Hi, my name is Joris van de Sande.&#xD;
&#xD;
&lt;span class="image right"&gt;&lt;img src="/blog/user/files/images/digger.gif" alt="Screenshot of Digger" title="Screenshot of Digger computer game"&gt;&lt;/span&gt; From the moment that I was born in 1983, I have always been interested in technology. My first experience with a PC was back in 1991 when my parents bought their first PC (based on an &lt;a href="http://en.wikipedia.org/wiki/Intel_80286"&gt;Intel 80286 processor&lt;/a&gt;). In these days I used the computer to play games like &lt;a href="http://en.wikipedia.org/wiki/Tetris"&gt;Tetris&lt;/a&gt;, &lt;a href="http://en.wikipedia.org/wiki/Digger_%28computer_game%29"&gt;Digger&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Space_invaders"&gt;Space Invaders&lt;/a&gt; and to create documents in &lt;a href="http://en.wikipedia.org/wiki/Word_perfect"&gt;Word Perfect 5.1&lt;/a&gt; (for &lt;a href="http://en.wikipedia.org/wiki/DOS"&gt;DOS&lt;/a&gt;). &#xD;
&#xD;
Fast forward a few years (I think it must be 1996) and I was playing around with &lt;a href="http://en.wikipedia.org/wiki/DBase"&gt;DBase III&lt;/a&gt; databases. A couple of months later my dad learned me how to interact with the DBase databases via the programming language &lt;a href="http://en.wikipedia.org/wiki/Clipper_%28programming_language%29"&gt;Clipper&lt;/a&gt; and how to create a simple user interface with it. After some time my first useful program called "Taal" (which means "Language" in Dutch) was born. I used it to learn foreign languages like English and German.&#xD;
&#xD;
&lt;span class="image left"&gt;&lt;a href="http://www.php.net"&gt;&lt;img src="/blog/user/files/images/php_logo.png" alt="PHP Logo" title="PHP Programming Language"&gt;&lt;/a&gt;&lt;/span&gt; In 1999 I created a simple HTML website for my group of friends, to let the world know about our existence. That was one of my first experiences with building websites. One year later I redesigned the website (with Dreamweaver) and I wanted the website to be more interactive, so I stumbled upon &lt;a href="http://www.php.net"&gt;PHP&lt;/a&gt; (version 3.x at that time) and started writing PHP scripts. Soon a &lt;a href="http://www.mysql.com/"&gt;MySql&lt;/a&gt; database was in use too.&#xD;
&#xD;
&lt;span class="image right"&gt;&lt;img src="/blog/user/files/images/aboutme/softwaredesign_gone_wrong.jpg" alt="Software Design gone Wrong" title="Software Design gone Wrong (&amp;copy; geeksaresexy.net)"&gt;&lt;br&gt;&lt;small&gt;&amp;copy; &lt;a href="http://www.geeksaresexy.net/gas-cartoons/"&gt;geeksaresexy.net&lt;/a&gt;&lt;/small&gt;&lt;/span&gt; A couple of years, programs and websites later I went to college and I received my bachelor degree in Computer Science in 2005. In November 2005, I started working as a Junior Application Programmer at &lt;a href="http://www.prezent.nl"&gt;Prezent Internet&lt;/a&gt;. At that time I was mainly working on customer websites (build in &lt;a href="http://www.php.net"&gt;PHP&lt;/a&gt;), creating frontends and new modules for the CMS that was build in house.&#xD;
&#xD;
Today I work as a Software Architect at &lt;a href="http://www.prezent.nl"&gt;Prezent Internet&lt;/a&gt;. I am responsible for the architecture of most web applications that are build by Prezent Internet today. Besides that I spend my days coding (&lt;a href="http://www.php.net"&gt;PHP&lt;/a&gt;, JavaScript, HTML, CSS and MySql), teaching my colleagues about software design and object oriented programming, maintaining the CMS and sometimes I even act as a system administrator (for Windows and Linux).&#xD;
&#xD;
When I have some spare time I like to listen to music, watch (action) movies or one of my favorite TV series (&lt;a href="http://www.cbs.com/primetime/numb3rs/"&gt;Numb3rs&lt;/a&gt;, &lt;a href="http://www.cbs.com/primetime/ncis/"&gt;NCIS&lt;/a&gt; and &lt;a href="http://www.fox.com/house/"&gt;House&lt;/a&gt;), drink some beer with my friends, visit a dance festival, browse the Internet for interesting (open source) projects and other stuff that I am interested in, play with different operating systems and I like programming. </description><pubDate>Fri, 14 Aug 09 21:30:52 +0200</pubDate><guid isPermaLink="false">tag:habari.vandesande.no-ip.org,2009:about/1250278276</guid></item></channel></rss>

