Drupal Tip: How to Move Serialized Data from One Table to Another Table

We were trying to insert serialized array data into a table, using the module.install update. We then noticed that we couldn't unserialize the data afterwards. After a little looking around, we realized that we cannot use the function "update_sql()" to insert the serialized data.

The problem is the pre-constructed SQL statement. When we use the function "udate_sql($sql)", we construct the SQL statement without using any argument. A serialized data normally contains curly brackets. When you call update_sql($sql), it then hands your pre-constructed $sql to the function db_query($sql). Inside the function db_query(), it replace the curly bracket with table prefix blindly, even the curly bracket inside data string are converted. And thus you will not be able to unserialize the data from the table anymore.

To get around this, instead of calling update_sql, call db_query($sql, $args). Put all the variables to be inserted into the table into the argument list. This way db_query will only convert the curly bracket surrounding the table name.

 

Comments

tyuyj

I'm thankful to the one who wrote this passage. I always read and write this style of articles. Also, as a daily writer, I present my respects to the all writers. Lately, I have watched a video resembling that in facebook. I research in all areas.

In my opinion, people should research first and write then.

Regards....

Thanks!

Thanks so much for this useful comment. I was beating my head up against a wall of Drupal arrogance and the painful lack of clear documentation.

Much appreciated!

I understand the pain

Drupal's documentation does need some good work.

If drupal allows us to post comments to the api directly, it might help a bit.