A little while ago I posted about storing more than one piece of information in a MySQL table field. This can help with storing arrays of data that do not need to be included in a relationship.
Anyway the trick here is to first place the information you want to store in your MySQL table into an array, so let’s for an example take URL’s & their alt text.
$info = array('http://wordpress.org' => 'A excellent blogging platform',
'http://reddit.com' => 'The home page of the internet');
I think you get the idea. So normally storing this info in a database would require two fields and two rows. However you can store this in a single field by using
serialize(), this compacts the array into a byte string so that you can store it in a single field. All you need to do when it is retrieved from the database is use
unserialize() and it will be turned back into an array again. Here is a quick example:
$info = array(...) //the elipsis represents the information written above.
$serial_info = serialize($info);
// Now we store the info inside $serial_info into a database...
// Ok. Now we have retrieved the information from the db field 'links' into a var named $row
$info = unserialize($row['links']);
That would give you back an array, like the one you started with, inside the variable
$info. Just remember that when you serialize an array or object the resulting byte string can be rather large so you will probably need to store it in a suitably large field type, text is the most commonly used.
I feel I should add some updated information on this. Using serialization to store data is very helpful, but please be weary. If you are going to need to search through the data in some way, or be able to find a row by data stored in a serialized field, do not serialize it. Any data that will need to be used to find an item should be stored in a separate column. If you are going to be storing a lot of abstract data that might be needed to be searched upon, then you might want to consider whether a traditional MySQL database is correct for your application.
Hopefully that was helpful. Please remember to only use this in situations where it would be useful. It should not be a replacement for making tables when they would be needed.