Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unserializing data

Tags:

database

php

I have some order info stored in a DB table row called "order_details". From what research I've done so far, the cells contain a serialized array:

s:346:"{"total":50,"tax_perc":8.25,"shipping_total":20,"discount_total":0,"discount":"","pick_up":0,"items":[{"id":"0","amount":1,"available":1000,"price":50,"first_pet":50,"pet_count":1,"size":"4x6","type":"portrait","clothe":"93","total":50}],"size_found":false,"taxes_total":4.13,"grand_total":74.13}";

I tried using unserialize() but that didn't print anything. Leaving it serialized at least printed the contents of the cell.

Any ideas on why nothing is being printed when I use unserialize()?

Also, I'm trying to create a back-end interface using this data, so given that unserialize() will organize this data, I need to be able to show only a particular element (like ID). Is that possible? Or will it have to print all of that data?

UPDATE Here's how what I have so far works: (1) When someone places an order, the data is serialized and thrown into the db and an email is sent to both me and the client (2) For this Admin page where I want to ask for and show said serialized data, I query the db to return the relevant table ordered by id (3) Then I have this, which is meant to throw that data into a table row on the UI.

$mydata = $all_orders[$i]['order_details'];
$mydata = unserialize($mydata);
echo '<td>' . $mydata . '</td>';

At this point I am just trying to get a better visual on that data. I was also hoping to be able to maybe sort the table on price, ID, etc.

Thanks in advance for your responses!

like image 694
coatandtails Avatar asked Mar 07 '26 02:03

coatandtails


1 Answers

The strings in your database have been JSON encoded to a string, then serialized. That's why the first character in the serialized string is an "s" (s means whatever follows is a string, in your case, of 346 characters in length).

The reason that unserialize() is returning false is because the string that has been serialized contains unescaped double quotes. When unserialize() runs, it fails as it uses the first 2 sets of double quotes to determine the string to unserialize. In your case, unserialize() is trying to unserialize the following string:

s:346:"{"

This causes a failure, as the string between the two sets of double quotes isn't 346 characters long.

You shouldn't need to serialize something that has already been JSON encoded. Choose one of the encoding options (JSON encoding or serializing) but not both, and stick with the one that you choose.

like image 86
David Smith Avatar answered Mar 09 '26 14:03

David Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!