February 19, 201016 yr Hi, I am trying to insert an array into a mySQL database using the serialize function. When I echo the function onto the page it works and I get this: a:1:{i:0;s:3:"111";} However, when I insert this exact same variable into my dabase, it becomes null and I get this: a:1:{i:0;N;} I am using wordpress and this is my code: $serialized_xer = serialize($album_array); echo $serialized_xer; $wpdb->query("UPDATE $wpdb->nggalbum SET sortorder = '$serialized_xer' WHERE id = '$album_id' "); All help is greatly appreciated. Thanks
February 19, 201016 yr What sort of field is it and what length does it allow? Also it might be worth using mysql_real_escape_string on it to escape characters like the " If the field type and length is fine, then perhaps its worth considering base64 encoding and seeing if that inserts correctly
February 19, 201016 yr Author The field type is longtext so it should take it ok. Ive tried both mysql_real_escape_string and base64 encoding without any success. I get the same error. Thanks - any ideas welcome.
February 19, 201016 yr Can you change your code to $serialized_xer = serialize($album_array); echo $serialized_xer; $query = "UPDATE $wpdb->nggalbum SET sortorder = '$serialized_xer' WHERE id = '$album_id' "; echo $query; $wpdb->query($query); and paste the query here I have a feeling this might be a scope issue
February 19, 201016 yr Author I get the following error: WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1' at line 1] 1 Any ideas how to resolve this? Thanks for your help.
February 19, 201016 yr So it doesn't echo out the query at all??? That is highly odd. Can you paste the whole of your code block for this
February 19, 201016 yr here... $serialized_xer = serialize($album_array); $serialized_xer = mysql_real_escape_string($serialized_xer); $query = "UPDATE `$wpdb->nggalbum` SET `sortorder` = '$serialized_xer' WHERE `id` = '$album_id' "; echo $query; $wpdb->query($query); If the above doesn't work $serialized_xer = serialize($album_array); $serialized_xer = base64_encode($serialized_xer); $query = "UPDATE `$wpdb->nggalbum` SET `sortorder` = '$serialized_xer' WHERE `id` = '$album_id' "; echo $query; $wpdb->query($query);
February 22, 201016 yr Author Ive tried both of those already with no luck. Any ideas anyone please? Could it be something to do with the fact that I need to create a class?
Create an account or sign in to comment