Username: Save?
Password:
Home Forum Links Search Login Register*
    News: Keep The TechnoWorldInc.com Community Clean: Read Guidelines Here.
Recent Updates
[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 24, 2024, 11:48:22 AM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[April 03, 2024, 06:11:00 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[March 06, 2024, 02:45:27 PM]

[February 14, 2024, 02:00:39 PM]
Subscriptions
Get Latest Tech Updates For Free!
Resources
   Travelikers
   Funistan
   PrettyGalz
   Techlap
   FreeThemes
   Videsta
   Glamistan
   BachatMela
   GlamGalz
   Techzug
   Vidsage
   Funzug
   WorldHostInc
   Funfani
   FilmyMama
   Uploaded.Tech
   MegaPixelShop
   Netens
   Funotic
   FreeJobsInc
   FilesPark
Participate in the fastest growing Technical Encyclopedia! This website is 100% Free. Please register or login using the login box above if you have already registered. You will need to be logged in to reply, make new topics and to access all the areas. Registration is free! Click Here To Register.
+ Techno World Inc - The Best Technical Encyclopedia Online! » Forum » THE TECHNO CLUB [ TECHNOWORLDINC.COM ] » Programming Zone » PHP
 Serialize this - Saving Objects in PHP
Pages: [1]   Go Down
  Print  
Author Topic: Serialize this - Saving Objects in PHP  (Read 984 times)
Daniel Franklin
TWI Hero
**********


Karma: 3
Offline Offline

Posts: 16647


View Profile Email
Serialize this - Saving Objects in PHP
« Posted: September 26, 2007, 12:41:05 PM »



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');";

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`) ) TYPE=MyISAM AUTO_INCREMENT=1 ;


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);

if(parent::getNumRows($result)>0){ $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.

Articles Source - Free Articles

Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Copyright © 2006-2023 TechnoWorldInc.com. All Rights Reserved. Privacy Policy | Disclaimer
Page created in 0.579 seconds with 24 queries.