Save php array to mysql database [duplicate]

2

I need to save an array in a field in the database table and I would like to know how best to do this if using serialize() and unserialize() or json_encode() and json_decode() . I know you have other alternatives too, for example, you can use explode and implode or even better create another auxiliary table for that, but this option to create the auxiliary table has already been discarded and the other one I think would be the worst option.

    
asked by anonymous 02.09.2015 / 04:18

1 answer

1

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

But that's about to come back from saving an array to a table. I use quite a few times mainly when I want to save import logs from Excel.

I usually do serialize to save and unserialize when I need to work the array again. I've never had any problems.

The table may eventually become large depending on the type of the light array.

No advice explodes or implodes since with a serialized array you always have the array compressed and this is guaranteed. An unserialize and you have the array again as startup:)

I hope I have helped.

Greetings

    
02.09.2015 / 11:45