user983248
user983248

Reputation: 2688

unserialize or array slice?

What is the correct method to get each block of data from this entry on my database ?

I'm customizing a plugin called custom contact forms for wordpress, and basically for each email received a new entry is created on the database with the following format:

s:8:"enq_name";s:3:"ICE";s:9:"enq_email";s:23:"[email protected]";s:9:"enq_phone";s:10:"9191919191";s:11:"enq_message";s:74:"bla bla bla bla and more bla bla bla";s:10:"enq_footer";s:288:"This message has been set to you by my";

How can I get individual values like for example the message enq_message

Apologies if one or both methods mentioned on the title of the question are wrong, I don't have a clue how to do this :(

Thanks in advance

Edit: Thanks to fulhack I now have this...

$query = "SELECT * FROM `$table_name` WHERE `data_formid` = $ff ORDER BY `$table_name`.`id` ASC LIMIT 0, 5";     
$result = mysql_query($query) or die(mysql_error());
    while($row = mysql_fetch_array($result)){
    echo $myDate = date( 'd/M/Y', $row['data_time'] );
    $string = $row['data_value'];

    $serialized = $string;
    $unserialized = unserialize($serialized);
    $theValue = $unserialized['enq_email'];

    echo $theValue;
    }

But this only echoes 'e' or more exaclty 'eeeee' since there is for entries on the database.

Upvotes: 0

Views: 353

Answers (1)

Jonatan
Jonatan

Reputation: 2886

Try using unserialize() ( http://www.php.net/manual/en/function.unserialize.php ), and then use the key (enq_message in your case) to retrieve the value you're looking for.

EDIT: I don't have access to a PHP installation, but I think this would work:

$serialized = serialize(array("key" => "value")); // This is the data you supplied
$unserialized = unserialize($serialized);
$theValue = $unserialized["key"];

You should probably add some error-checking though.

Upvotes: 2

Related Questions