Learn to study
 
Aug 29, 2005 - 12:05 PM
 
Main Menu

Online
There are 2 unregistered users and 0 registered users on-line.

You can log-in or register for a user account here.


Our Partners:
PHP Programming: Serialize This - Saving Objects In PHP
Monday, June 06, 2005 - 06:57 PM

Printer-friendly page Send this story to someone

Website DevelopmentWhen 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?

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="";
if(isset($xwordInfo['xword_name'])){
$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`,
`xword_height`,`xword_date`)
VALUES ('', '" . $data . "', '$name', '$owner',
'$width', '$height', '$today');";

Note: About the Author:
Kevin Davies is the webmaster of Crossword Heaven (http://www.crosswordheaven.com) - 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.




 
Login
 





 Log in Problems?
 New User? Sign Up!

Related links
· More about Website Development
· News by ovlazare


Most-read story in Website Development:
Common PHP Tasks and Questions


<< 1 2 >>
Serialize This - Saving Objects In PHP | Log-in or register a new user account | 0 Comments
Comments are statements made by the person that posted them.
They do not necessarily represent the opinions of the site editor.
© WebsiteDevelopmentNews.com
Flash Templates and Web Templates