mysql arrays

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
wickedbob
Forum Newbie
Posts: 5
Joined: Tue Aug 06, 2002 11:22 pm

mysql arrays

Post by wickedbob »

duh! newbie question:

can mysql store data in tables as an array?
and if so, what code do i use to insert this data into a table?

cheers

BoB.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

This is what you would want to do. You would start with the array that you wanted to store. Let's call it $array.

$array = array();

$array_to_store = serialize($array);

// call mysql_query() and insert the $array_to_store into the table

This will insert a serialized version of the array into the mysql table. Now when you wish to retrieve the array from the mysql table, do this:

// retrieve the row from the table

$array = unserialize($value_from_database); // where $value_from_database is from the database
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

An alternative way (if you don't need to keep the associative keys of the array) is to use implode() and explode():

Code: Select all

// Create string from array
$db_data = implode('|', $array);
That'll create a pipe-separated-list of the array data. To get it back into an array:

Code: Select all

// Create array from pipe-separated-list
$array = explode('|', $db_data);
Mac
wickedbob
Forum Newbie
Posts: 5
Joined: Tue Aug 06, 2002 11:22 pm

Post by wickedbob »

i want to be able to select information from the database that meet certain criteria - will serializing/imploding data in tables make it difficult/impossible to list as criteria to select within an mysql query?

BoB.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

in that case, i'd stick with using implode() and explode() ... they aren't that hard after you use them a bit
Post Reply