Wednesday, October 19, 2011

serialize Data in PHP Drupal

If we have an array
$ary=array();
$ary['alt']="Cordy icon";
$ary['title']="Cordy";

then its serialize output "serialize($ary)" will be something like this:
a:2:{s:3:"alt";s:10:"Cordy icon";s:5:"title";s:5:"Cordy";}

on unserialize(a:2:{s:3:"alt";s:10:"Cordy icon";s:5:"title";s:5:"Cordy";})
it will give Array
(
[alt] => Cordy icon
[title] => Cordy
)

In drupal if we are using cck field then we are getting this serialize data.
So to insert query will be somthing like this
db_query("INSERT INTO {my_table} (name, description, array1) VALUES ('something', 'something else', '%s')",serialize($ary));

or
db_query("update {my_table} set array1='%s' where name='something'",serialize($ary));

Note: Remember to keep tablename inside {} then it will work in drupal.

coz Drupal uses {} arround the tables names, to be able to do some manipulations on those names -- like prefix them, if you have configured it to do so. So, you must not use {} in your query -- except arround tables names,

No comments:

Post a Comment