PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




serialize

Name

serialize — Converts data into an easily-storable and transportable format.

Synopsis

string serialize(data);
mixed data: Data to be serialized

Returns

String representation of the data

Description

serialize() converts a variable or value into a format that can easily be stored as plain text and then restored to its original value using unserialize() . Both the variable's type and value are stored.

Any variable type can be serialized, except resource pointers. Also, only an object's member variables are serialized - the object's methods are discarded during the process.

Version

PHP Version: 3.0.5+, 4+

Example

Example 1404. Basic use of serialize()

$quote = <<<_END_QUOTE_
"Almost at once the No. 37 Penpoint returned to the Featureless Expanse."<br />
&nbsp;&nbsp;&nbsp;&nbsp;An Inanimate Tragedy, Edward Gorey
_END_QUOTE_;

$serialized_quote = serialize ($quote);

$unserialized_quote = unserialize ($serialized_quote);

print <<<_END_
<b>Before serialization:</b>
    <blockquote>$gorey_quote</blockquote>

<b>After serialization:</b>
    <blockquote>$serialized_quote</blockquote>

<b>After unserialization:</b>
    <blockquote>$unserialized_quote</blockquote>
_END_;

Example 1405. Use serialize() to cache post requests to web pages

<?
// Store most of the data from a post request in a database

// database connection code omitted for brevity
// ...

// We don't want to store the user's password, so unset that variable
unset ($HTTP_POST_VARS['password']);

// Serialize the $HTTP_POST_VARS array
// Use addslashes() to prevent any quotes in the serialized data from
// accidentally 'breaking' the quoting in the database query
$serialized_data = addslashes (serialize ($HTTP_POST_VARS));

// Insert the data into a MySQL database table
$query = "INSERT INTO post_cache (user, data, page, post_time)
    VALUES ('$user', '$serialized_data', '$PHP_SELF', NULL)";

@ mysql_query ($query)
    or die ("Query <b>$query</b> failed.
        This error message was generated: <b>" . mysql_error () . '</b>');

// ...

// Retrieve the post data from the database
$query = "SELECT user, data, post_time, page FROM post_cache
    ORDER BY post_time";

$result = @ mysql_query ($query)
    or die ("Query <b>$query</b> failed.
        This error message was generated: <b>" . mysql_error () . '</b>');

// Display the stored post data
while ($temp = mysql_fetch_row ($result))
{
    list ($user, $data, $post_time, $page) = $temp;

    // Strip the slashes from the serialized data
    // ... and convert it back into a PHP value
    $unserialized_data = unserialize (stripslashes ($data));

    print "User <b>$user</b> made the following post request to page
        <b>$page</b> on <b>$post_time</b>: <blockquote><pre>";

    print_r ($unserialized_data);

    print "</pre></blockquote><br/ >";
}
?>



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.