Jump to content
MakeWebGames

Serializing data


Floydian

Recommended Posts

I'm sure lots of folks already know how to serialize data using PHP. I'm sure there's plenty of debate to be had about when and where to use serialized data.

My purpose here is this:

I made a script that demonstrates how to handle serialized data for a friend. Just the basics. And since I made the demo script, I figured I might as well post it for anyone that might find it helpful.

As with many things, this one deal isn't a cure all. It's not going to turn your world upside down. But it may be the perfect solution for one problem.

The script is commented, and it should be fairly self explanatory. Running this script requires a valid mysql connection in order for the db escaping to work.

Here's a link to a working example of this script:

www.steelbreeze.us

 

<?php
error_reporting(E_ALL);

// You need a valid mysql connection to run this file
mysql_connect('localhost', 'xxxxxxxxxxxx', 'xxxxxxxxxxx');

// This represents an items_bartered column and one row (i.e., items bartered on one item)
// One master array of item/userid pairs, so
// Doing a foreach on the master array ($items) gives a $value['item_id'] and $value['them_id']
$items = array(
array('item_id' => 10, 'them_id' => 34),
array('item_id' => 56, 'them_id' => 154),
array('item_id' => 67, 'them_id' => 576),
array('item_id' => 13, 'them_id' => 359),
array('item_id' => 67, 'them_id' => 207),
array('item_id' => 78, 'them_id' => 833),
array('item_id' => 2, 'them_id' => 488),
);

// serialize the array

$serialized_items = serialize($items);

echo "<h3>Serialized array</h3>";
echo $serialized_items .'

';

// We need to escape this before storing it in the db, or it will fail
$serialized_items_esc = mysql_real_escape_string($serialized_items);


echo "<h3>Serialized and db escaped array</h3>";
echo $serialized_items_esc .'

';


/*
Please note that I'm not using the db escaped array. Since storing that _string_
in the db will remove the slashes added during the escaping, we won't have slashes
to deal with when unserializing.
*/
// Now we'll unserialize the array:
$unserialized_items = unserialize($serialized_items);


echo "<h3>Unserialized array</h3>";
var_export($unserialized_items);

// Now, what happens if the string we tried to unserialize couldn't be unserialized?

echo "<h3>Unserialize error</h3>";
$string = 'this string can not be unserialized';
unserialize($string);

echo "<h3>Unserialize error suppression</h3>";
$string2 = 'this string can not be unserialized';
$string2 = @unserialize($string2);
echo "The variable we unserialized couldn't be unserialized, thus the unserialize function outputs: ";
var_dump($string2);


echo "<h3>Handling the new variable</h3>";
/*
Since we will either get a value from our unserialized variable or a boolean false, that leaves the door open
for that result to be anything from a string, to a number, an array, or an object. Thus we need to check
if the variable is an object.
*/

echo '

$string2 could not be unserialized...</p>';
if (!is_array($string2)) {
echo "\$string2 is not an array...";
} else {
// do the foreach loop here
}

echo '

$unserialized_items was successfully unserialized...</p>';
if (!is_array($unserialized_items)) {
echo "\$unserialized_items is not an array...";
} else {
foreach ($unserialized_items as $value) {
	var_export($value);
	echo '
';
}
}

Link to comment
Share on other sites

Re: Serializing data

All depends what you want. If you want fast serialization as well as deserialization using PHP serialize things are certainly faster than going recursively inside arrays or objects. Now, as Vali pointed out, you will not be able to search via the database things inside those structures. Another problem with serialization, is that normally it works only for a given language. Imagine you have a Java and a PHP code accessing the same objects, well will not work if you serialize using the PHP function, unless you code the same function inside Java.

All that however is not here important.

Link to comment
Share on other sites

Re: Serializing data

@ Vali, that's a very good point. And definitely worth mentioning.

@ a_bertrand, I normally recommend using JSON when one needs to serialize data that will be needed for use in languages other than PHP. Another good point for sure.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...