Register Members List Search Today's Posts Mark Forums Read

Reply
 
Article Options
The joys of serializing data
filburt1
Join Date: Feb 2002
Posts: 6,144

Maryland, US
by filburt1 filburt1 is offline 18 Jun 2003

To serialize a variable or object in PHP means to take the variable or object and convert it to a string that can be stored anywhere, including, relevant to vB, a MySQL database. Later, it can be unserialized, at which point it'll be (nearly) exactly the same as when it was serialized.

An example:

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Remember to addslashes $serialized when making a query because it could contain quotes. Also note that some tiny things can be lost when serializing; namely, internal array pointers, among others.

Reference:
http://www.php.net/serialize
http://www.php.net/unserialize (way faster than serializing)

Last edited by filburt1; 09 Jul 2003 at 13:02..
Views: 5597
Reply With Quote
Comments
  #2  
Old 18 Jun 2003, 01:11
MUG MUG is offline
 
Join Date: Apr 2002
Note that you can't serialize resources (such as a mysql connection) so the example posted won't work.
Reply With Quote
  #3  
Old 18 Jun 2003, 01:39
filburt1 filburt1 is offline
 
Join Date: Feb 2002
The example itself should work, though, because $DB_site is just an object that never stores a result handle (at least not permanently).
Reply With Quote
  #4  
Old 18 Jun 2003, 01:42
MUG MUG is offline
 
Join Date: Apr 2002
Today at 09:39 PM filburt1 said this in Post #3
The example itself should work, though, because $DB_site is just an object that never stores a result handle (at least not permanently).
One of the variables in $DB_site is $link_id and that can't be serialized.
$query_id can't be serialized either.

In order to run $DB_site->query(), $link_id has to be a valid MySQL connection resource.

Last edited by MUG; 18 Jun 2003 at 01:44.
Reply With Quote
  #5  
Old 18 Jun 2003, 02:03
filburt1 filburt1 is offline
 
Join Date: Feb 2002
Oh, good point

But the concept is there
Reply With Quote
  #6  
Old 12 Feb 2004, 08:33
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Real name: Rob
Great! A thread on serializing.

I'm just starting to learn this. Maybe you can answer this question, filburt. How do we determine this when making the query for the datstore?


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

That's a snippet of caode we're using. What are the s:1 and the s:9 (and s:14) for? I know the zero is for the default value. And is there an easier way to list this so it can be added to easily?
Reply With Quote
  #7  
Old 12 Feb 2004, 09:41
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Real name: Andreas
s(tring):(Length)1:(Value)"0";s(tring):(Length)14:(Value)"getthreadviews";

I'd guess.
Reply With Quote
  #8  
Old 12 Feb 2004, 10:49
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Real name: Rob
Why would you need 2 string lengths in there?
Reply With Quote
  #9  
Old 12 Feb 2004, 10:59
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Real name: Andreas

Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

Arn't there two strings - "0" (length 1) and "getthreadviews" (length 14)?
Reply With Quote
  #10  
Old 12 Feb 2004, 11:40
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Real name: Rob
Ahhh, ok, now I understand.

Thank you, sir.
Reply With Quote
  #11  
Old 12 Feb 2004, 17:48
futureal futureal is offline
 
Join Date: Feb 2002
I think the two strings are actually part of a single variable. For example:

s:8:"newusers";s:1:"0";s:14:"getthreadviews";s:1:"0";

This would correspond to a variable called "newusers" with a value of "0" and a variable called "getthreadviews" with a value of "0" as well.

When it is processed by the unserialize(...) function it looks for pairs like this and converts them into PHP variables in an array. So if you called:

$test = unserialize(the above string);

You should then have:

$test[newusers] == 0
$test[getthreadviews] == 0

Hope that helps!
__________________
Editor, Xtreme RC Cars Magazine
Owner, R/C Tech
Reply With Quote
  #12  
Old 12 Feb 2004, 18:03
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Real name: Rob
Thank you, very much, for the explanation. I'm just now learning about this great function and it's not turning out to be as confusing as I first thought, it seems. It's starting to make some sense to me.
Reply With Quote
  #13  
Old 13 Feb 2004, 02:32
Link14716's Avatar
Link14716 Link14716 is offline
 
Join Date: Jun 2002
Real name: Matthew Gordon
Yeah, serializing is way cool.
Reply With Quote
  #14  
Old 13 Feb 2004, 02:35
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Real name: Rob
I'm having a blast with it. I finished one hack upgrade with it and I'm working on another. Still stumbling and learning, though.
Reply With Quote
  #15  
Old 13 Apr 2004, 17:01
AN-net's Avatar
AN-net AN-net is offline
 
Join Date: Dec 2003
when should we really use serialize?
Reply With Quote
Reply



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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


New To Site? Need Help?

All times are GMT. The time now is 07:42.

Layout Options | Width: Wide Color: