NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > Webmaster Tutorials
Reload this Page PHP: Using serialize to handle and store arrays

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.

Advanced Search
0 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 01-14-2007, 10:17 AM THREAD STARTER               #1 (permalink)
Senior Member
Join Date: Dec 2006
Location: England
Posts: 1,565
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'); 
????: NamePros.com http://www.namepros.com/webmaster-tutorials/281176-php-using-serialize-handle-store-arrays.html
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:"va lue3";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'
????: NamePros.com http://www.namepros.com/showthread.php?t=281176
        
); 
            
            

$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.
Matthew. is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 12:59 AM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger