MySQL and PHP data store method

Hello, I have seen on wordpress and joomla that data is sometimes stored in the database by using a certain method that allows a lot of info (normally related to the cms’ settings) to be stored in one record.

What is its purpouse? How does it work? How is the info retrieved using PHP? Do you know of any links to tutorials on this issue?

Thanks for any feedback provided !!

This is a wordpress example:

a:2:{i:2;a:4{s:5:“title”;s:0:“”;s:5:“count”;i:0;s:12:“hierarchical”;i:0;s:8:“dropdown”;i:0;}s:12:“_multiwidget”;i:1;}

This is a joomla example:

__default|a:9:{s:22:“session.client.browser”;s:88:“Mozilla/5.0 (Windows; U; Windows NT 6.1; es-MX; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3”;s:15:“session.counter”;i:16;s:8:“registry”;O:9:“JRegistry”:3:{s:17:“_defaultNameSpace”;s:7:“session”;s:9:“_registry”;a:2:{s:7:“session”;a:1:{s:4:“data”;O:8:“stdClass”:0:{}}s:11:“application”;a:1:{s:4:“data”;O:8:“stdClass”:1:{s:4:“lang”;s:0:“”;}}}s:7:“_errors”;a:0:{}}s:4:“user”;O:5:“JUser”:19:{s:2:“id";s:2:“62”;s:4:“name”;s:13:“Administrator”;s:8:“username”;s:5:“admin”;s:5:“email”;s:20:"raguilarml@gmail.com”;s:8:“password”;s:65:“b6271c3e20561b78a04ce424d9ac2782:IabmRo9LVgQC9bzR2rvzZqEa7DXnFOy9”;s:14:“password_clear”;s:0:“”;s:8:“usertype”;s:19:“Super Administrator”;s:5:“block”;s:1:“0”;s:9:“sendEmail”;s:1:“1”;s:3:“gid”;s:2:“25”;s:12:“registerDate”;s:19:“2010-10-28 17:34:53”;s:13:“lastvisitDate”;s:19:“2010-10-28 22:35:35”;s:10:“activation”;s:0:“”;s:6:“params”;s:0:“”;s:3:“aid”;i:2;s:5:“guest”;i:0;s:7:“_params”;O:10:“JParameter”:7:{s:4:“_raw”;s:0:“”;s:4:“_xml”;N;s:9:“_elements”;a:0:{}s:12:“_elementPath”;a:1:{i:0;s:127:“C:\xampp\htdocs\contraste\sitioweb\rt_syndicate_oct2010\rt_syndicate_j15-rocketlauncher\libraries\joomla\html\parameter\element”;}s:17:“_defaultNameSpace”;s:8:“_default”;s:9:“_registry”;a:1:{s:8:“_default”;a:1:{s:4:“data”;O:8:“stdClass”:0:{}}}s:7:“_errors”;a:0:{}}s:9:“_errorMsg”;N;s:7:“_errors”;a:0:{}}s:13:“session.token”;s:32:“38839502897b892d7691c3c4bdcaf2e4”;s:19:“session.timer.start”;i:1288366788;s:18:“session.timer.last”;i:1288367303;s:17:“session.timer.now”;i:1288367303;s:23:“gantry-current-template”;s:16:“rt_syndicate_j15”;}

Hi,

This is called serialization. It is useful for storing arrays into text fields.

$array = array(
	'parameters' => 'abcdef',
	'items'		=> 'lots of them',
	'something' => 'thanks',
	array (
			'mutli-dimensional' => 'Hey hey',
	)
);

// Now compact the data
$data = serialize($array);

// This gives us:
// a:4:{s:10:"parameters";s:6:"abcdef";s:5:"items";s:12:"lots of them";s:9:"something";s:6:"thanks";i:0;a:1:{s:17:"mutli-dimensional";s:7:"Hey hey";}}

When its all compact (like what you’re seeing) you’d insert the data into the database.

Then if you fetch the data with mysql select tou can unserialize it the same way:


// Just a plain example ...
$result =mysql_fetch_array($query); 
$data = unserialize($result);

// Youll have that same array to work with, nice!
$array = array(
	'parameters' => 'abcdef',
	'items'		=> 'lots of them',
	'something' => 'thanks',
	array (
			'mutli-dimensional' => 'Hey hey',
	)
);

Or you can watch this 3 minute Youtube PHP Serialize Video

nice video, jesse

serialize is seriously cool… right up until the point where you want to search for one of the serialized items, and then you’re s.o.l.

can you say “table scan”

:smiley: