Your Ad Here

Source code
RSS feeds
Wordpress plugins
Sample Chapters
Script directory
Our Links

low cost magazines
software directory
domain names
dzphp editor
rapidphp 2011
Webmaster Resources
Only PHP - Get the best scripts NOW!
AndreaPHP Programming

Valid XHTML 1.0 Transitional

Valid CSS!

Serialize this - Saving Objects in PHP

When building my website "Crossword Heaven" I came across a problem. I created a PHP object called "crossword" but needed to save the information in the object to a database. Now considering that this object contained a lot of information this was not an easy thing to do. Or was it?

The answer: serialize().

What the serialize() function does is take something like an array or object and converts it into a string that can be stored in a database. All I had to do so that I could save the crossword object is something like "serialize($crossword)." Easy! Some words of warning though. If you're using a version of PHP less than version 4 watch out because only properties get saved, not methods.

Here's a peek at the actual code:

$data = addslashes(serialize($crossword));
$name = $xwordInfo['xword_name'];

$today = date('Y-m-d H-i-s');

$sql = "INSERT INTO `xword` ( `xword_id` , `xword_obj` , `xword_name`, `xword_owner`, `xword_width`,
VALUES ('', '" . $data . "', '$name', '$owner', '$width', '$height', '$today');";

And here's the SQL to create the table 'xword':

CREATE TABLE `xword` (
`xword_id` int(11) NOT NULL auto_increment,
`xword_obj` blob NOT NULL,
`xword_name` varchar(100) NOT NULL default '',
`xword_owner` varchar(100) NOT NULL default '',
`xword_width` int(11) NOT NULL default '0',
`xword_height` int(11) NOT NULL default '0',
`xword_date` datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (`xword_id`)

You'll see that I used the addSlashes() function. That's because when the crossword object was serialized it contained characters like double quotes. These had to be escaped before the crossword could be saved to the database.

Now having saved a crossword object to a database I had to have a way to get it back. Surely, if there was a method to serialize an object there had to be one to unserialize an object, right? And yes, there is: unserialize().

As you'd expect, unserialize() works the same way as serialize(), but in the opposite direction. You give it some serialized data and it returns the thing that was serialized. To get the crossword back all I had to do was something like "unserialize($crosswordData)."

Here's a look at the code:

$xwordId = (get_magic_quotes_gpc()) ? $xwordId : addslashes($xwordId);

$sql = "SELECT xword_id, xword_obj, xword_name, xword_age from w3b_xword where xword_id=$xwordId";
$result = parent::getSQL($sql);
$row = parent::getRow($result);

$crossword = unserialize($row['xword_obj']);

And that's it. Obviously serialize() and unserialize() are pretty handy functions to have around. And in my case I couldn't do without them.

About the Author

Kevin Davies is the webmaster of Crossword Heaven ( - a site that lets you create and solve crosswords online. If you're a webmaster you can also link to crosswords. Crosswords are a great way to keep your visitors on your site and keep them returning.

Written by: Kevin Davies


Here is our complete list of articles

Apache MySQL and PHP for Windows

Apache MySQL PHP for Windows

ASP CGI and PHP Scripts and Record Locking What Every Webmaster Needs To Know

Autoresponders With PHP

Bring Your Web Site to Life With PHP

Clickbank Security Using PHP

Create a Simple Effective PHP Form for Your Web Site

Creating Dynamic Website Content with PHP MySQL

Developing a Login System with PHP and MySQL

Developing State enabled Applications With PHP

For Automated Sites PHP and MySQL are A Perfect Match

Get PHP pages indexed in the Search engines

Getting your Visitors Details Using PHP

How PHP Can Help Save You Time And Mistakes

How to make a simple form mailer with PHP

HTACCESS Wrappers with PHP

Maguma has integrated support for the eBay Acellerator Toolkit for PHP PHP AT in their PHP IDE Maguma Workbench

Make a Search Engine For Your Website With PHP

Mastering Regular Expressions in PHP

More Autoresponders With PHP

MySQL Database Handling in PHP

ONLINE the eBay Accelerator Toolkit for PHP PHP AT for the PHP IDE Maguma Workbench is available

Password Protection and File Inclusion With PHP

Password Protection with PHP MySQL and Session Variables

PHP Account Activation

PHP and Cookies a Good Mix

PHP Auto surf Websites

PHP Databases

PHP Dynamic Content

PHP Email

PHP Encryption

PHP Error Pages

PHP Files

PHP Form Series Part 1 Validators Client side Validation

PHP General Scripting

PHP Image Gallery

PHP in the Command Line

PHP is 10 years old

PHP my favorite Server side Programming Technology

PHP On The Fly

PHP Pear Packages Why they are so important to php developers

PHP Redirect

PHP Scripts Dont Have to End in PHP

PHP Server to Client with No Refresh

PHP Sessions / Cookies

PHP Templates

PHP User Login Authentication

PIM Team Case Study Creating Text Effects With PHP and GD

Protecting your HTML and PHP Source Code

Quick Intro to PHP Development

Simple Solution for Php Includes IFrames

Site Personalization With PHP

Some PHP functions you must know

Track Your Visitors Using PHP

Victoryvisions The PHP/Mysql Company Complete website development company


 Random Code
PHP and cURL example(internet)
using the array_splice function(beginner)
Break out of a loop(beginner)
 Random Article
Programming resources
Tutorials directory
Hosting resources
ASP site
Domain names
Maxi directory
bigarticle : free articles
A Code
Code N Tutorials
Programmers help

beginners PHP Copyright © 2004 onwards by beginnersPHP.