Database: Storing Data Via Serialize and Unserialize



Serialize Database: This is also data storage alternative. You can use serialize() and and unserialize(), that convert a PHP variable into a storable (text) format and convert it back again. These functions are often used to store variables into databases, pass them between programs, or other such purposes. In this post, we use to create a file data storage.

<?php
$orig = array(
    array('name' => 'Apple', 'category' => 'Fruit'),
    array('name' => 'Grape', 'category' => 'Fruit'),
    array('name' => 'Elephant', 'category' => 'Animal'),
    array('name' => 'Dog', 'category' => 'Animal')
    );

file_put_contents('output.ize', serialize($orig), LOCK_EX);

$new = unserialize(file_get_contents('output.ize'));

echo "First Enter is: Name: {$new[0]['name']} Category: {$new[0]['category']}<br />";

echo '<pre>';
var_dump($new);
echo '</pre>';
?>




Tag: database, serialize Category: PHP Database Post : February 05th 2008 Read: 940 Bookmark and Share

blog comments powered by Disqus