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
Anonymous
May 8, 2009
2 years 39 weeks
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....
Anonymous
August 3, 2009
2 years 26 weeks
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!
voyageur
August 10, 2009
2 years 25 weeks
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.
Anonymous
April 29, 2011
40 weeks 4 days
db_query is not the way to go
db_query is not the way to go in a module.install file. The whole point of update_sql is to pass back any errors during update so you know if something went wrong. The better solution is to make your own function that perfroms the same function but is serialize() safe. If you can, the best thing to do is report the problem to the project issue queue, and submit a patch that fixes the problem. http://drupal.org/node/667714