Page 1 of 1

mysql arrays

Posted: Tue Aug 06, 2002 11:22 pm
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.

Posted: Tue Aug 06, 2002 11:51 pm
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

Posted: Wed Aug 07, 2002 1:49 am
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

Posted: Wed Aug 07, 2002 6:48 am
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.

Posted: Wed Aug 07, 2002 9:07 am
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