Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

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

 

Maryland, US
Show Printable Version Email this Page Subscription
filburt1 filburt1 is offline 06-17-2003, 10:00 PM

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:
PHP Code:
$serialized serialize($myobject);
.
.
.
$myobjectagain unserialize($serialized);
$myobjectagain->method(); 
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)
Reply With Quote
  #12  
Old 02-12-2004, 04:48 PM
futureal futureal is offline
 
Join Date: Feb 2002
Location: Del Mar, CA, USA
Posts: 556
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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!
Reply With Quote
  #13  
Old 02-12-2004, 05:03 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
  #14  
Old 02-13-2004, 01:32 AM
Link14716's Avatar
Link14716 Link14716 is offline
 
Join Date: Jun 2002
Location: Georgia, USA
Posts: 2,519
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yeah, serializing is way cool.
Reply With Quote
  #15  
Old 02-13-2004, 01:35 AM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

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
  #16  
Old 04-13-2004, 04:01 PM
AN-net's Avatar
AN-net AN-net is offline
 
Join Date: Dec 2003
Location: AnimationTalk.com
Posts: 2,367
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

when should we really use serialize?
Reply With Quote
  #17  
Old 04-13-2004, 04:17 PM
Xenon's Avatar
Xenon Xenon is offline
 
Join Date: Oct 2001
Location: Bavaria
Posts: 12,878
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

when you read out an object very often, but don't change that object very often
Reply With Quote
  #18  
Old 04-13-2004, 04:23 PM
AN-net's Avatar
AN-net AN-net is offline
 
Join Date: Dec 2003
Location: AnimationTalk.com
Posts: 2,367
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

can you give me an example, cause im kinda new to this serialize thingy
Reply With Quote
  #19  
Old 04-13-2004, 06:49 PM
Boofo's Avatar
Boofo Boofo is offline
 
Join Date: Mar 2002
Location: Des Moines, IA (USA)
Posts: 15,776
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Like my forumhome stats cache hack. It gets read from the cache only every 10 minutes on my site (you can set it to whatvere you want) so it's a great place to store it since the information won't change until it gets updated again. Plus, when you unserialize it (read from the cache) it doesn't produce a query.
Reply With Quote
  #20  
Old 10-05-2007, 07:46 AM
kafi kafi is offline
 
Join Date: Apr 2004
Posts: 379
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

How do I make querry to filter users according their option in multiple profile field option?.

I need to get all users that have choosed any of first five options out of 17? Some of them may have choosed more than 1 otion!
In admincp you can serach users only like this "option1 and option2 and option3..." but I need to search like this "option1 OR option2 OR option3" (which is impossible with admincp).

a:18:{i:0;s:8:"option1";i:1;s:19:"option2";i:2;s:5 :"option3";i:3;s:15:"option4";i:4;s:21:"option5 ";i :5;s:15:"";i:6;s:16:"option6";i:7;s:17:"option7";i :8;s:10:"option8";i:9;s:18:"option9";i:10;s:8:"opt ion10";i:11;s:15:"option11 ";i:12;s:14:"option12";i:13;s:7:"option13";i:1 4;s: 7:"option14";i:15;s:20:"option15";i:16;s:22:"opt io n16";i:17;s:3:"option17";}

Thanks!!!!!
Reply With Quote
  #21  
Old 10-05-2007, 08:20 AM
Andreas's Avatar
Andreas Andreas is offline
 
Join Date: Jan 2004
Location: Germany
Posts: 6,863
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

[sql]
SELECT user.*
FROM user
LEFT JOIN userfield ON (userfield.userid=user.userid)
WHERE (fieldX & 1) OR (fieldx & 2) OR (fieldX & 4)
[/sql]

Alternatively, you could add up the values
[sql]
SELECT user.*
FROM user
LEFT JOIN userfield ON (userfield.userid=user.userid)
WHERE (fieldX & 7)
[/sql]

This query should be more efficient.

Option 1 = 1
Option 2 = 2
Option 3 = 4
Option 4 = 8
and so on
Reply With Quote
Reply


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

Forum Jump


All times are GMT. The time now is 01:16 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.07235 seconds
  • Memory Usage 2,297KB
  • Queries Executed 25 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (2)pagenav_pagelink
  • (11)post_thanks_box
  • (11)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (11)post_thanks_postbit_info
  • (10)postbit
  • (11)postbit_onlinestatus
  • (11)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete