The joys of serializing data
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:
Reference: http://www.php.net/serialize http://www.php.net/unserialize (way faster than serializing) |
Note that you can't serialize resources (such as a mysql connection) so the example posted won't work.
|
The example itself should work, though, because $DB_site is just an object that never stores a result handle (at least not permanently).
|
Quote:
$query_id can't be serialized either. In order to run $DB_site->query(), $link_id has to be a valid MySQL connection resource. |
Oh, good point ;)
But the concept is there ;) |
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? PHP Code:
|
s(tring):(Length)1:(Value)"0";s(tring):(Length)14:(Value)"getthreadviews";
I'd guess. |
Why would you need 2 string lengths in there? ;)
|
PHP Code:
|
Ahhh, ok, now I understand. ;)
Thank you, sir. ;) |
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! |
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. ;)
|
Yeah, serializing is way cool. :)
|
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. ;)
|
when should we really use serialize?
|
when you read out an object very often, but don't change that object very often :)
|
can you give me an example, cause im kinda new to this serialize thingy:)
|
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.
|
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!!!!! |
[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 |
Thank you Andreas!
It helped me today again :) |
Not to resurrect an old thread, but... I have been storing a lot of serialized data lately, and came across a pretty useful web tool to easily view it when needed. Basically This tool enables you to serialize or unserialize arrays (mainly) with several 'serialization' functions : php serialize, json, yaml, and php var_export.
Supposedly also does following: •unserialization of broken serialized array (recover / repair "php serialize" arrays) •deep unserialization of serialized serialisation ! ;) (for php serialize) http://www.unserialize.net/serialize |
All times are GMT. The time now is 10:49 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 | |
---|---|
|
|
More Information | |
Template Usage:
Phrase Groups Available:
|
Included Files:
Hooks Called:
|