Re: saving and retrieving an array from a database

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, August 7, 2006 7:52 am, ross@xxxxxxxxxxxxx wrote:
> ok this seem to work but how do I bring it back? This is what I have
> so far.
>
> <?
> $first[] = array('appple', 'pear', 'banana');
> $second = serialize($first);

Store $second in the DB here.

It's just a big long-ass string at this point.

You can use echo $second; to see what it looks like.


[cue clock fast-forward visual sequence]

Pull $second (or whatever you want to call it) out of the database.

It's just a big long string.

> $third[]= unserialize($second);

This de-constructs that big string into an array, and you are back in
business.

> echo $second; //outputs serialized data
> echo $third[1];
>
> ?>

NOTE:
90% of the time, you should be putting each array item into the
database separately, so you can use SQL to retrieve only the bits you
need instead of schlepping the whole thing back and forth all the
time...

If you are new to programming, think long and hard about what you will
be doing with these array elements, and if you plan on using PHP to
search/sort/filter them at some point, don't do that.  Put them in as
individual data in the DB.

If you always use the whole array, and never search it, sort it, or
filter it down to fewer elements, and are just shuffling it
back-and-forth, then serializing it and storing it en masse is fine.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux