2

I want to store short arrays in a field. (I realize there are reasons to break the array up into items and store separately, but I'm opting for a simple storage option at the expense of less capability.)

My code to create the array is as follows:

$str = "one,two,three,four";
$array = explode (",",$str)

I then store into a text field in mysql using an insert statement It seems to store fine. In PhPAdmin, it shows ARRAY in the field and if I just echo $array, it prints "ARRAY".

The problem is occurring when I try to retrieve data.

I am retrieving using

while($row = mysql_fetch_array($res)) { 
$array = $row['list']; //that's the field it is stored in 
echo $array; // echoes "ARRAY"

//so far so good. However, when I then try to print out the contents of the array, I get error messages. I have tried using implode and also for each.

$text = implode(",", $array);//yields error message improper argument in implode function
foreach($array as $val) {
    echo $val;
} //yields error message improper argument for for each statement

}

Could my entry in the dbase not be a proper array? What could the problem be? Thanks for any suggestions.

user1260310
  • 2,229
  • 9
  • 49
  • 67

3 Answers3

2

The usual approach to storing an array in this way is to serialize the data before input, and unserialize it upon retrieval.

$array = array('one', 'two', 'three', 'four');
$stringToStore = serialize($array);

Later:

while($row = mysql_fetch_array($res)) {
  $array = unserialize($row['list']);
  var_dump($array);
}
cantlin
  • 3,236
  • 3
  • 21
  • 22
  • It went in as an array, but I gather without serializing, it was not something I could work with. For now, I just stored and retrieved the string as in answer below. I need to study get a better handle on serialization. – user1260310 Apr 06 '12 at 21:27
1

What you're inserting is not an array, just what PHP has evaluated your array as being in string form. To store an array in MySQL without properly normalizing your data you'll need to serialize it. Basically you'd want to do something like:

$serialized = implode(',', $arrayToStore);

and then store that in MySQL. On its way out then you'll do:

$unserialized = explode(',', $arrayFromMySQL);
clexmond
  • 1,509
  • 13
  • 20
0

I think you can use serialize and also (if you don't use serialize) if your array string is as follows

$str = "one,two,three,four";

then why you are making it an array before inserting it into your database, I think you can insert the string directly and when you need to use your string as an array then you can simply retrieve the string from database and make it an array using explode like

while($row = mysql_fetch_array($res)) { 
$array = explode(",", $row['list']); // "one,two,three,four"
echo $array[0]; // one
The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • Thanks, this did the trick. I cannot upvote you as I don't have enough points but this was the way to go...No real need to go to an array here as the string contains the same info. – user1260310 Apr 06 '12 at 21:21
  • You are welcome and I can understand your limitations for voting me up but may be 15 is enough to up vote and if it's not then no problem, I'm satisfied. :-) – The Alpha Apr 06 '12 at 21:29