Dynadot

PHP: Using serialize to handle and store arrays

NameSilo
Watch
This is a tutorial i wrote a while back.
Note: It's a little out of date with my current coding habits so you will find some code here is not as good as it should be. But the basic theory is there.



Intro:

Ok, in this tutorial im going to cover a really handy function, called serialize();.

This function basically turns data., such as arrays into a string that can be ut anywhere, used anyhow etc etc.
This string can then be put back (or docoded) to its previous form.

Part One

OK, first were going to do the basics, im not going to explain it because its irrelevant to the tutorial.

1: Connect to a database.

PHP:
<?php

// using constants for security.
define('_user', 'username');
define('_pass', 'password');
define('_host', 'localhost'); 
define('_database', 'database');

@mysql_connect(_host, _user, _pass) or die( mysql_error() );
@mysql_select_db( _database ) or die( mysql_error() );


// Thats it for now.....
?>
The rest...

Ok, so we've connected.

Now to get our array to which we want to put into the database. Normally this would be derived from an explode(); or other function/process to create an array. But lets manually create one:

PHP:
$data = array(
		'1' => 'value1',
		'2' => 'value2',
		'3' => 'value3',
		'4' => 'value4'
		);


So now is where we get to the serialize(); part.

Lucky for us, serialize has only one part to it.

serialize
( mixed value )

So lets input our data string into the serialize function:

The file so far:

PHP:
<?php

// using constants for security.
define('_user', 'username');
define('_pass', 'password');
define('_host', 'localhost'); 
define('_database', 'database');

@mysql_connect(_host, _user, _pass) or die( mysql_error() );
@mysql_select_db( _database ) or die( mysql_error() );
	
	
	
$data = array(
		'1' => 'value1',
		'2' => 'value2',
		'3' => 'value3',
		'4' => 'value4'
		); 
			
$newstring = serialize( $data );
	
/* This will return:
@
@	a:4:{i:1;s:6:"value1";i:2;s:6:"value2";i:3;s:6:"value3";i:4;s:6:"value4";}
@
*/
?>

As i commented, $newstring will now have a value of
a:4:{i:1;s:6:"value1";i:2;s:6:"value2";i:3;s:6:"value3";i:4;s:6:"value4";}


So, now to prove it works, lets put it into the database and output it again.

I wont explain this, as again this is not part of the tutorial, and you should know it.

PHP:
<?php

// using constants for security.
define('_user', 'username');
define('_pass', 'password');
define('_host', 'localhost'); 
define('_database', 'database');

@mysql_connect(_host, _user, _pass) or die( mysql_error() );
@mysql_select_db( _database ) or die( mysql_error() );
	
	
	
$data = array(
		'1' => 'value1',
		'2' => 'value2',
		'3' => 'value3',
			'4' => 'value4'
		); 
			
			

$newstring = serialize( $data );
	
/* This will return:
@
@	a:4:{i:1;s:6:"value1";i:2;s:6:"value2";i:3;s:6:"value3";i:4;s:6:"value4";}
@
*/
	
			 
@mysql_query("INSERT INTO `example` (`id`, `data`) VALUES('', $newstring)") or die('Error: ' . mysql_error());
			
			
?>

The data should now be in the table. So now lets output it:

PHP:
<?php
// using constants for security.
define('_user', 'username');
define('_pass', 'password');
define('_host', 'localhost'); 
define('_database', 'database');

@mysql_connect(_host, _user, _pass) or die( mysql_error() );
@mysql_select_db( _database ) or die( mysql_error() );

		
$query = @mysql_query("SELECT `data` FROM `example` LIMIT 0,1") or die('Error ' . mysql_error());
	
$foo = mysql_fetch_array($query);
	
echo 'This came out of the database: '. $foo['data'];
	
// The old data from DB
$oldstring = $foo['data'];
	
?>

OK, so we can see its in there but what do we do with it? Ok, lets use logic

To do it we used: serialize();

To undo it we will use: unserialize();

So lets see it!!!!

PHP:
$changedstring = unserialize( $oldstring );

Ok, so lets test it!!!

Using loops to get all the data to prove it worked!!

PHP:
foreach ($changedstring as $key => $value)
{
					  
	echo $key .' - '. $value .'<br />';
			
}


Ok so that's it! The below are completed files from this tutorial, hopefully you will learn from them.



Complete file 1:
PHP:
<?php

// using constants for security.
define('_user', 'username');
define('_pass', 'password');
define('_host', 'localhost'); 
define('_database', 'database');

@mysql_connect(_host, _user, _pass) or die( mysql_error() );
@mysql_select_db( _database ) or die( mysql_error() );
	
	
	
$data = array(
		'1' => 'value1',
		'2' => 'value2',
		'3' => 'value3',
		'4' => 'value4'
		); 
			
			

$newstring = serialize( $data );
	
/* This will return:
@
@	a:4:{i:1;s:6:"value1";i:2;s:6:"value2";i:3;s:6:"value3";i:4;s:6:"value4";}
@
*/
	
			 
@mysql_query("INSERT INTO `example` (`id`, `data`) VALUES('', $newstring)") or die('Error: ' . mysql_error());
			
			
?>




Complete file 2!!

PHP:
<?php

// using constants for security.
define('_user', 'username');
define('_pass', 'password');
define('_host', 'localhost'); 
define('_database', 'database');

@mysql_connect(_host, _user, _pass) or die( mysql_error() );
@mysql_select_db( _database ) or die( mysql_error() );


		
$query = @mysql_query("SELECT `data` FROM `example` LIMIT 0,1") or die('Error ' . mysql_error());
	
$foo = mysql_fetch_array($query);
	
echo 'This came out of the database: '. $foo['data'];
	
// The old data from DB
$oldstring = $foo['data'];
	
$changedstring = unserialize( $oldstring );
	
// Lets test it!!
	
foreach ($changedstring as $key => $value)
{
					  
	echo $key .' - '. $value .'<br />';
			
}
			
?>


Hopefully you will now know how to manage and store arrays through sterilization.

Any questions/comments post below and i or someone else will get around to replying :)

Matt.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back