<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>devexp &#187; database</title>
	<atom:link href="http://www.devexp.eu/tag/database/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devexp.eu</link>
	<description>DEVelopment EXPerience, shared with the world!</description>
	<lastBuildDate>Fri, 28 Oct 2011 12:07:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Storing multiple data in 1 database field with PHP serialize / unserialize</title>
		<link>http://www.devexp.eu/2008/10/08/storing-multiple-data-in-1-database-field-with-php-serialize-unserialize/</link>
		<comments>http://www.devexp.eu/2008/10/08/storing-multiple-data-in-1-database-field-with-php-serialize-unserialize/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 10:45:47 +0000</pubDate>
		<dc:creator>Van de Voorde Toni</dc:creator>
				<category><![CDATA[database]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[serialize]]></category>

		<guid isPermaLink="false">http://www.devexp.eu/?p=163</guid>
		<description><![CDATA[While the serialize/unserialize functions exists since the early PHP 4 version, I ignored its existence. I discovered it recently doing some research on how to store multiple data in 1 database field. Before continuing, keep in mind that it&#8217;s not &#8230; <a href="http://www.devexp.eu/2008/10/08/storing-multiple-data-in-1-database-field-with-php-serialize-unserialize/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>While the serialize/unserialize functions exists since the early PHP 4 version, I ignored its existence. I discovered it recently doing some research on how to store multiple data in 1 database field.</p>
<p>Before continuing, keep in mind that it&#8217;s not always recommended to store multiple data in a database field, because it will be very difficult, if not impossible, to create queries using the inserted data. All serialized data will have to be unserialized, which takes time.</p>
<p>But why did I use it ?</p>
<p>The website I work on has a widget system (like netvibes and igoogle). Each widget has default settings that can be altered by a user. Those changes need to be saved in a database, so the next time the user loads the widget it will make use of the new settings.</p>
<p>When I first had to create this, I did not know of serialization in php, so I created the following db structure. (trying to respect DB Normalization).</p>
<div id="attachment_164" class="wp-caption aligncenter" style="width: 510px"><a href="http://www.devexp.eu/wp-content/uploads/2008/10/serialize-widgetsettings.png"><img class="size-full wp-image-164" title="widget and settings" src="http://www.devexp.eu/wp-content/uploads/2008/10/serialize-widgetsettings.png" alt="widget and settings" width="500" height="126" /></a><p class="wp-caption-text">widget and settings</p></div>
<p>You can see that I have a table with a list of widgets and a list of possible settings. Those two tables are connected to a widgetSetting table where the value of a setting to a widget is stored. It worked perfectly but it is not so proficient :</p>
<p>* To retrieve a widget&#8217;s settings, you need to join 2 tables.<br />
* To insert new widget settings, you need to check the settings table if a setting exists or not.<br />
* When updating widget settings, you need to check if a setting is still used or not.</p>
<p>This is a lot of work and database complexity for something that only is used for saving/retrieving settings of a widget. In my case it would be much better if I could save those settings in 1 field in the widget table.</p>
<div id="attachment_165" class="wp-caption aligncenter" style="width: 185px"><a href="http://www.devexp.eu/wp-content/uploads/2008/10/serialize-widget.png"><img class="size-full wp-image-165" title="widget" src="http://www.devexp.eu/wp-content/uploads/2008/10/serialize-widget.png" alt="widget" width="175" height="157" /></a><p class="wp-caption-text">widget</p></div>
<p>This is how I serialize my settings :</p>
<pre class="brush: php; title: ; notranslate">
$settings = array(
'itemsOnPage' =&gt; 30,
'showChart' =&gt; true,
'startDate' =&gt; '01-10-2008',
'endDate' =&gt; '31-10-2008'
);

$serializedSettings = serialize($settings);

/**
* outputs:
*
* a:4:{s:11:&quot;itemsOnPage&quot;;i:30;s:9:&quot;showChart&quot;;b:1;s:9:&quot;startDate&quot;;s:10:&quot;01-10-2008&quot;;s:7:&quot;endDate&quot;;s:10:&quot;31-10-2008&quot;;}
*
*/</pre>
<p>The serialize function will generate a storable representation of my array.<br />
Just save this string in a database field and the job is done. To rebuild the original array from this value you only need to call the unserialize function:</p>
<pre class="brush: php; title: ; notranslate"> $storedRepresentation = 'a:4:{s:11:&quot;itemsOnPage&quot;;i:30;s:9:&quot;showChart&quot;;b:1;s:9:&quot;startDate&quot;;s:10:&quot;01-10-2008&quot;;s:7:&quot;endDate&quot;;s:10:&quot;31-10-2008&quot;;}';
$settings = unserialize($storedRepresentation);

/*
* print_r($settings) would give :
*
* Array
* (
*  [itemsOnPage] =&gt; 30
*  [showChart] =&gt; 1
*  [startDate] =&gt; 01-10-2008
*  [endDate] =&gt; 31-10-2008
* )
*/</pre>
<p>As you can see the unserialize function correctly rebuild the original array.</p>
<p>It is also possible to serialize Objects with the exception of the built-in objects.</p>
<pre class="brush: php; title: ; notranslate">class MySettings {

private
$itemsOnPage = 30,
$showCart = true,
$startDate = '01-10-2008',
$endDate = '31-10-2008';

public function getItemsOnPage() {
return $this-&gt;itemsOnPage;
}
}

$mySettingsSerialized = serialize(new MySettings());

$mySettings = unserialize($mySettingsSerialized);

echo $mySettings-&gt;getItemsOnPage(); // 30</pre>
<p>When serializing objects, PHP will attempt to call the member function __sleep() prior to serialization. This is to allow the object to do any last minute clean-up, etc. prior to being serialized. Likewise, when the object is restored using unserialize() the __wakeup() member function is called.</p>
<pre class="brush: php; title: ; notranslate">class MySettings {

private
$itemsOnPage = 30,
$showCart = true,
$startDate = '01-10-2008',
$endDate = '31-10-2008';

public function getItemsOnPage() {
return $this-&gt;itemsOnPage;
}

public function __wakeup() {
$this-&gt;itemsOnPage *= 10;
}
}

$mySettingsSerialized = serialize(new MySettings());

$mySettings = unserialize($mySettingsSerialized);

echo $mySettings-&gt;getItemsOnPage(); // 300</pre>
<p>So you see it is very simple to use the seriailize/unserialize functions in php. But like I said in the beginning of the post, use it wisely when storing the serialized value in a database. I only use it when I do not need the value for creating sql queries.</p>
<div class="shr-publisher-163"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.devexp.eu/2008/10/08/storing-multiple-data-in-1-database-field-with-php-serialize-unserialize/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

