[SOLVED] Storing checkbox data in MySQL with serialize()
Posted: Tue Nov 28, 2006 3:54 pm
Hello everyone
I'm gradually converting our databases from FileMaker into MySQL by adding more and more features to our intranet interface.
I'm fairly new to MySQL but I've been using PHP for a couple of years now.
My question relates to the way that you can store checkbox data in MySQL.
In FileMaker if you have a field that can have multiple options via checkboxes, then each one is stored on its own line as text in that field.
Because I have a field that can have up to 20 possible options checked, what's the best way of storing this data in MySQL?
I don't really want to have 20 separate fields - one for each checkbox.
The checkboxes will always be manipulated as an array in PHP, so is it a good idea for me to use PHP's serialize() function to store the contents of the array in one MySQL field.
Then when accessing the data all I need to do is unserialize() and I have the data straight as an array.
The only downside to doing this is that it's probably not best practice in MySQL - as the data is being stored in a format that can only be used by PHP.
Also there is some storage overhead with serialize() - with the various square brackets newlines and other formatting that it adds to the data.
Any suggestions / opinions on this one?
Thanks
Ben
I'm gradually converting our databases from FileMaker into MySQL by adding more and more features to our intranet interface.
I'm fairly new to MySQL but I've been using PHP for a couple of years now.
My question relates to the way that you can store checkbox data in MySQL.
In FileMaker if you have a field that can have multiple options via checkboxes, then each one is stored on its own line as text in that field.
Because I have a field that can have up to 20 possible options checked, what's the best way of storing this data in MySQL?
I don't really want to have 20 separate fields - one for each checkbox.
The checkboxes will always be manipulated as an array in PHP, so is it a good idea for me to use PHP's serialize() function to store the contents of the array in one MySQL field.
Then when accessing the data all I need to do is unserialize() and I have the data straight as an array.
The only downside to doing this is that it's probably not best practice in MySQL - as the data is being stored in a format that can only be used by PHP.
Also there is some storage overhead with serialize() - with the various square brackets newlines and other formatting that it adds to the data.
Any suggestions / opinions on this one?
Thanks
Ben