3

This is my first time here and I have been pulling my hair out over this one, so I thought it would be a good first question for me.

I am saving some array data to a mysql database, and then later I am using unserialize to edit it. The problem is that it blanks out every other index in the array when I edit just one index. Here is some example code:

foreach($post as $key => $value)  {
     if (isset($row)) {
        if ($i > 2) { $tempArray = unserialize($row[$i]); }
     }

     $tempArray[$time] = $value;

     if ($key == 'pid' || $key == 'uid') { $data[$key] = $value; }
     else { $data[$key] = serialize($tempArray); }
     $i += 1;
     unset($tempArray);
  }

Thanks for any insight you can give.

4
  • 4
    For starters, you shouldn't be inserting serialized arrays into databases. Now, where is the rest of the code? What does the $tempArray look like, and how are you saving this data? Commented Aug 28, 2010 at 0:43
  • How would I go about inserting array data into my database then? And $tempArray is a temporary array used only to edit the saved array after I use unserialize. Commented Aug 28, 2010 at 0:50
  • 6
    You don't. Create another table and use a foreign key to link them together. You can always get what you want using JOINs. Commented Aug 28, 2010 at 0:55
  • @NullUserException Thank you for the help, I will look into using that method instead. Commented Aug 28, 2010 at 0:58

1 Answer 1

3

to store serialized array data in mysql and to retrieve and save it back to the same table, here is how you have to do it.

Fetch the serialized data from the table and unserialize it and store the array data inside a variable. make changes to the array data by using the variable we stored the data.

Again we serialize the variable having the modified array and update the mysql table field with the new serialized data.

For example:

$data = array(
   "uid" => "some value",
   "pid" => "some value"
);

$store_in_database = serialize($data);
/* ...do the mysql insert query to store the new data in the field (serializedfield)... */

Now to update the serialize data stored in the database table field.

/* ... fetch the row of the table or records any way you like (single record or multiple) */

foreach($db_result as $db_row){

$temp_array = $db_row['serializedfield'];
$temp_array['uid'] = "a new value";
$temp_array['pid'] = "another new value";

/* ... i wanted to add another array value */
$temp_array['akey'] = "some value";
$store_database = serialize($temp_array);
/* ... do the mysql update query to replace this new data with the old one. */
unset($temp_array);

}

The above is just to give you an idea. I hope you have understood how it should be done here.

Sign up to request clarification or add additional context in comments.

3 Comments

Storing serialized data in an RDBMS table is almost never what you want to do. It hides the data from the RDBMS itself, so you can't use any of the vast power of SQL to search by or in any way manipulate that data. If you want to do a "has many" relationship with the items that are in the array within the database, put them in their own table, with a foreign key back to the containing item.
So what ? He just asked how to do it, not to write a book on good code practises. Maybe there is just no need to approach that data in a relational way at all.
@GlennPlas Thanks for the support, and like you have said, the question doesn't ask any sort of relational data, it clearly stated serializing and unserializing of data, the design of a database is a different story. Mark-reed must have misunderstood that point. The answer may be terrible for some as there will be different ways to achieve the same result but instead commenting "terrible idea", it is best to show the right way Nulluserexception

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.