Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

serialize() problem

Featured Replies

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

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

  • 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.

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

  • 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.

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

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);

  • 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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.