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.
mysql arrays
Moderator: General Moderators
- protokol
- Forum Contributor
- Posts: 353
- Joined: Fri Jun 21, 2002 7:00 pm
- Location: Cleveland, OH
- Contact:
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
$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
- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK
An alternative way (if you don't need to keep the associative keys of the array) is to use implode() and explode():
That'll create a pipe-separated-list of the array data. To get it back into an array:
Mac
Code: Select all
// Create string from array
$db_data = implode('|', $array);Code: Select all
// Create array from pipe-separated-list
$array = explode('|', $db_data);- hob_goblin
- Forum Regular
- Posts: 978
- Joined: Sun Apr 28, 2002 9:53 pm
- Contact: