[advanced search]
Results from the most recent live auction are here.
15 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Webmaster Tutorials
User Name
Password

Old 01-14-2007, 10:17 AM   · #1
Matthew.
Stud Sausage
 
Location: England
Trader Rating: (25)
Join Date: Dec 2006
Posts: 1,545
NP$: 32.41 (Donate)
Matthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud ofMatthew. has much to be proud of
Adoption Breast Cancer Breast Cancer Cancer Survivorship
PHP: Using serialize to handle and store arrays

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 Code:
<?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 Code:
$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 Code:
<?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
Quote:
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 Code:
<?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 Code:
<?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 Code:
$changedstring = unserialize( $oldstring );


Ok, so lets test it!!!

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

PHP Code:
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 Code:
<?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 Code:
<?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.


Please register or log-in into NamePros to hide ads
__________________
My NamePros Tools
(firefox plugin, google gadget etc)
Matthew. is online now   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
Exdon free webhosting Find out how!
Advertise your business at NamePros
All times are GMT -7. The time now is 11:14 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0