Jump to content


Photo
- - - - -

Using seralize to handle arrays


  • Please log in to reply
No replies to this topic

#1 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 14 May 2006 - 03:23 PM

This is a tutorial i wrote a while back, enjoy. ;)

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

$config = array(
			 'user' => 'blah',
			 'pass' => '',
			 'DB' => 'test_database',
			 'host' => 'localhost'
			); 

@mysql_connect($config['host'], $config['user'], $config['pass']) or die( mysql_error() );
@mysql_select_db( $config['DB'] ) 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:

	
$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

$config = array(
		'user' => 'blah',
		'pass' => '',
		'DB' => 'test_database',
		'host' => 'localhost'
		); 

@mysql_connect($config['host'], $config['user'], $config['pass']) or die(mysql_error());
@mysql_select_db($config['DB']) 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

$config = array(
		'user' => 'blah',
		'pass' => '',
		'DB' => 'test_database',
		'host' => 'localhost'
		); 

@mysql_connect($config['host'], $config['user'], $config['pass']) or die(mysql_error());
@mysql_select_db($config['DB']) 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

$config = array(
			 'user' => 'blah',
			 'pass' => '',
			 'DB' => 'test_database',
			 'host' => 'localhost'
			); 

@mysql_connect($config['host'], $config['user'], $config['pass']) or die(mysql_error());
@mysql_select_db($config['DB']) 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!!!!

$changedstring = unserialize( $oldstring );

Ok, so lets test it!!!

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

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


WERE FINISHED!



Complete file 1:
<?php

$config = array(
		'user' => 'blah',
		'pass' => '',
		'DB' => 'test_database',
		'host' => 'localhost'
			); 

@mysql_connect($config['host'], $config['user'], $config['pass']) or die(mysql_error());
@mysql_select_db($config['DB']) 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

$config = array(
		'user' => 'blah',
		'pass' => '',
		'DB' => 'test_database',
		'host' => 'localhost'
		); 

@mysql_connect($config['host'], $config['user'], $config['pass']) or die(mysql_error());
@mysql_select_db($config['DB']) 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 />';
			
}
			
?>

Enjoy, and put to good use!!!!

Edited by Matthew., 30 September 2006 - 02:16 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users