Mixing Arrays and Strings in a Database Field

As I am typing this question, I think I may have figured out a solution, but hopefully someone out there has a better one.

I have built a simple database driven application for a client that currently has about 85 records in the database. And now the client has decided that he want one field (a category type of field) to be populated from a multiple select instead of a single select.

So from now on, the entries for this field will be arrays, instead of the strings that are there now. I am using the serialize and unserialize functions to deal with the arrays, but what do I do with the strings from before?

I’m thinking I should just write a short script to convert all the original entries into serialized arrays. Is that the best way to go about this?

Definitely NOT.

You should NEVER store two values in the one field in a database.

You need to redesign the database to add an extra table. So that the multiple values can all be stored separately.

4 Likes

Just wanted to second all of this. You REALLY want to get in the habit of this now. It will save you and anybody else that as to work on this project lots of headache later.

Normally I do that, but this change request came after the fact. Okay so I will set up my connecting table. Thank you.

That still doesn’t change the mess that would result if you tried to keep it in a single field.

The only difference from it having come after the fact is that you would price in the cost of the database change with the rest of the amendment (assuming you are charging for the change).

Definitely.

Also Known As… Working with Wordpress.

@Michael_Morris, I don’t quite understand the connection here with WordPress. This is not a website I am referring to. But I would be interested in hearing your reasoning because I do a lot of work with customizing WordPress. I am definitely open to new ideas.

WordPress has “serialize”
That is, it puts objects into some fields as strings
It “works”, but it is work

Wordpress does this exact sort of insane and infuriating serializing on its options table in order to store the settings for its plugins. It is one of the many reasons I consider it the worst PHP application ever written.

1 Like

It’s worth noting why serialized data in a database is bad rather than just saying it.

So why is it? Because it means the data cannot be queried. When using a database table with columns for each piece of data I can query those specific columns, it’s possible to find all records where a specific value is set or count how many times a value is set, group records by the records where that value is set. When storing serialized data all of this becomes impossible. The only way to search through the data is load ALL the records in PHP, unserialize them all, loop through each one and check the value you want. This is inefficient for both the computer and the programmer.

That’s what I thought when I saw this ‘serialize’ function.

So, not wanting to drift to far from the original topic, if I am at best intermediate in PHP skill level, and have this endless road of learning ahead, and find a php function that seems to do what I am looking for, or find several different solutions to an issue that are ‘highly recommended’, how do I decide whether my choice really is the best choice, or something that someone should never do (ie use ‘serialize’ and ‘unserialize’).

I find this extremely frustrating and often change direction in my personal projects just because some ‘expert in php’ has recommended a different approach.

Your reasoning, @TomB, makes a lot of sense. You go for the most efficient solution.

The frustration you are experiencing now is not likely to go away for quite some time, if ever:

You’re going to learn new things all the time. We all do. Here’s my take on this:

When you find a new technique for accomplishing a task (or any new technology for that matter), make sure you understand that new thing away from the situation you are currently presented with. Far too often, we find ourselves looking for an answer to a situation rather than to a problem.

For instance, your question had to do with how to convert single values into a serialized php array string representing an array. The actual problem you have though is needing to store multiple categories for a single entity.

Similarly, on many forums, users ask for help crafting a CLI command to achieve some desired effect. The situation that user is in dictates that he or she needs to accomplish a certain task, but more often than not, the real problem that user is facing is that they don’t have enough experience operating from the command line.

To be a little more pragmatic here, when you find yourself in this situation with PHP, here are some suggestions:

  • Separate the problem from your situation - Use that as your starting point for searching for a solution

  • Research alternatives to a solution you find - The PHP manual is especially helpful in this regard - if you find a new function or language construct you think will help solve your problem, make sure to read the comments (here’s one present on the manual page for the serialize function stating to not serialize data and place it into a database field (http://php.net/manual/en/function.serialize.php#107717))

  • Consistency is key - If you have solved the same problem in multiple ways throughout your application, but have determined a new solution is a better way to handle it, apply that solution to those other areas of your application.

  • Be willing to admit that you’ve made a mistake - This is a hard one to do, but it’s what really separates the amateur programmers from the professionals. If you realize that you’ve made a mistake, it will be a lot better if you go back and fix it as soon as you find it rather than wait until it cripples your application.

  • Understand that committing to be a programmer means committing to the future - If you want to be a programmer, you’ll never be able to quit learning new techniques. Technology and the best practices surrounding it change constantly and to ensure you remain relevant as time goes on, you have to be willing and able to constantly learn new things.

1 Like

The best advice that can be given to someone learning how to program - do NOT use Wordpress as a model for best practices. It breaks about every best practice principle that can be broken, gleefully. Even if you have to work with it, do not use it as a reference for how things should be done. Do use it as a reference for how not to program.

Oh I don’t. :smile:, @Michael_Morris

I just use WordPress as a tool for building some sites for clients. I also build sites and applications from scratch.

Even though my WordPress themes are all custom built from the ground up, I never go into the core code to ‘see how it should be done’. As a matter of fact, I was not even aware that WordPress uses the function ‘serialize’.

The application that prompted this thread has absolutely nothing to do with any WordPress site.

@myesain84, the main issue here is ‘don’t believe the experts until you’ve checked it out for yourself in php.net’.

Just to be clear: The serialize function is useful in some circumstances and on its own is not a problem. It’s how it’s used to store serialized data in the database that’s a problem.

1 Like

Even with a database there are times to use it - specifically cache tables using serialize is fair game. But it should never be used to store the definitive copy of the data.

1 Like

Or perhaps more appropriately: understand the solution you’ve discovered (or have been provided) before you apply it to your project.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.