Page 1 of 1

storing arrays in database fields

Posted: Mon Jul 16, 2007 5:27 am
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?

Posted: Mon Jul 16, 2007 5:34 am
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.

Posted: Mon Jul 16, 2007 5:39 am
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.