Save php array to mysql database

Asked

Viewed 3,467 times

2

I am needing to save an array in a field in the database table and would like to know how best to do this by using the serialize() and unserialize() or json_encode() and json_decode(). I know you have other alternatives for this as well, such as being able to use the explode and implode or even better create another auxiliary table for this, but this option to create the auxiliary table has already been discarded and the other I think would be the worst option.

  • you will need to do research in this field?

  • in this field in particular will have only numbers, which by the way, in each should not exceed 3 items and yes, it may be that I have to search in this field and for that I thought to use a like and it is certain that I will not need to do any Join.

  • There is a basic concept of not working what you don’t need, and depending on the functions you have described, you should use the most convenient for the type of data you want to work with. Since you put the tags php, mysql, it would be good to see a part of the code in question, so that we can better understand what it is.

1 answer

1

I usually use serialize. But attention, will the array be giant? If it is giant I advise you to change the maximum size of the variable in SQL to longtext instead of text/varchar etc... Otherwise you run the risk of thinking you are saving everything well and in the end you have a corrupted array in the table.

But there, coming back to the question of storing an array in a table. I use quite often mainly when I want to save Excel import logs.

I usually serialize to save and unserialize when I need to work the array again. I never had a problem.

The table may eventually get large depending on the type of the clear array.

No advice explode or implode since with a Serialized array you always have the compressed array and this is guaranteed. One unserialize and you have the array again as the start :)

I hope I’ve helped.

Regards

Browser other questions tagged

You are not signed in. Login or sign up in order to post.