storing arrays in database fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

storing arrays in database fields

Post by aceconcepts »

Hi,

I am having a little problem with my array and storing/retrieving it from a database field.

My array "$dbSession" stores the value of checkbox values from a form. The array works perfectly.

However, when I record the array variable into a database field named "arrSigs" (data type=text) it displays as "Array" within the database. I thought this would be normal as the value is in fact an array.

The problem arises when I try to extract and use the array from the database.

When I use the array extracted from the database as the argument in a foreach loop i get the following error: Warning: Invalid argument supplied for foreach().

Any suggestions?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

The implicit type cast array-string produces the string Array for all array, the content of the array is lost.
Either use something like serialize() or use an extra table to store each element of the array as one record.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Hi Volka,

Thanks for the rather prompt response.

I used serialize to store the array and then unserialize to use the array once extracted from the database.

Thanks for the suggestion it works perfectly.
Post Reply