I need to get the values from a serialized string, to use it on some statistics. I already got that info in other pages to show some content, but I have no idea why I'm having this problem now.

The DB has a table called wj_db7_forms (I'm using a WordPress plugin to log the inputs on a form).

And the column with the info I need is on form_value.

Cleaning the content of the column, there is a part with s:4:"city";s:6:"boston".

I want to get all the forms, and on every one, get the value for city. That value can have any lenght, because it's an open input.

I tried a lot of ways, but none worked.

I get erros like

Parse error: syntax error, unexpected 'city' (T_STRING)

or don't get errors at all, but the variable seems empty, etc.

I tried unserialize, array_pop, print_r, echo, everything...

global $wpdb;
$table_name = $wpdb->prefix.'db7_forms';
$results    = $wpdb->get_results( "SELECT form_value FROM $table_name WHERE form_post_id = 6062", OBJECT );
$form_data  = unserialize( $results[0]->form_value );

With that code, I get all the info from the form, but I get everything, including info from other columns... And only from one form. I need to get from all the forms with that 'form_post_id' that are inside 'wj_db7_forms' table.

The content inside the 'form_value' of one of the forms is here: https://pastebin.com/e6LzJzLS

$form_data['city'] works now. But I need to get the values for all the forms. It only returns one for me.

  • What is the exact value of that column? – kero Jul 13 '17 at 13:46
  • The value is: pastebin.com/e6LzJzLS I'll update the question with new info. – Marcelo Henriques Cortez Jul 13 '17 at 13:56
  • This is the output of $results[0]->form_value? Did you change some parts of that value you posted here (because it is not properly serialized now)? Anyway, are you sure $form_data['city'] doesn't work? What does your print_r() return? – kero Jul 13 '17 at 14:03
  • $form_data['city'] works now. But I need to get the values for all the forms. It only return one for me. – Marcelo Henriques Cortez Jul 13 '17 at 14:09

So basically the form saves a large serialized array in the database. Now since you have multiple forms, you need to iterate over the result from get_results, unserialize the column and then access the city key.

global $wpdb;
$table_name = $wpdb->prefix.'db7_forms';
$results = $wpdb->get_results( "SELECT form_value FROM $table_name WHERE form_post_id = 6062", OBJECT );
// iterate over results
foreach ($results as $result) {
    $data = unserialize($result->form_value);
    // now the city is in $data['city']
| improve this answer | |
  • It's probably right, but I ahve more than 1 form getting saved. That's why I used the 'form_post_id', cuz I need only the forms from that ID. (the 'form_post_id' isn't unique, all the forms sent from the same page, have the same 'form_post_id', but different ID's – Marcelo Henriques Cortez Jul 13 '17 at 14:21
  • @MarceloHenriquesCortez Okay, sorry I am not familiar with that plugin. So if you keep the query the same and only add the foreach, does it work? – kero Jul 13 '17 at 14:22
  • Got an error trying to place just the foreach: Notice: Array to string conversion in C:\xampp\htdocs\tophomestay\wp-content\plugins\contact-form-cfdb7\inc\add-ons.php on line 36 Array – Marcelo Henriques Cortez Jul 13 '17 at 14:25
  • Ok, now it doesnt show any erro msg, works, but it's still showing only 1 result – Marcelo Henriques Cortez Jul 13 '17 at 14:27
  • And now its working showing all. Thnx =) – Marcelo Henriques Cortez Jul 13 '17 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.