Solved

PHP5: how do I pull serialized data from db?

Posted on 2014-07-25
7
280 Views
Last Modified: 2014-07-25
Hello Experts,

Steps:
1. Insert serialized data into db
ex: $serializedSessionData = serialize($sessiondata);
it looks something like this:
a:3:{s:4:"time";a:1:{s:5:"start";s:5:"23:30";}s:2:"UA";a:1:{i:0;s:72:"Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Firefox/31.0";}s:2:"IP";a:1:{i:0;s:9:"127.0.0.1";}}

how do I pull serialized data from db? In other words, if I want to assign the "UA" value to a variable, how do I do that?

If I am too vague, please let me know I can try to clarify....

Regards,
0
Comment
Question by:epifanio67
  • 4
  • 3
7 Comments
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 40220671
You pull the serialized data, then use PHP unserialize().

But don't do that.  Instead use JSON.  It's a more universal notation than PHP serialized data, and it accomplishes the same thing.

If you want the value of "UA" you can get it from $sessiondata before you encode it into a string.
0
 

Author Comment

by:epifanio67
ID: 40220687
Thank you Ray,

I was afraid I had to unserialize the data first... too much code...
I was hoping to treat it as an obj array somehow...

I will try JSON...

Regards,
0
 

Author Closing Comment

by:epifanio67
ID: 40220688
Thank you Ray
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40220702
Thanks for the points.  When I've had to store large amounts of "serialized" data in the past (stuff from APIs and the like) I usually pulled out a few key search elements and put them into separate columns of the table.  Then you can store the long JSON string as a "text" data type.  If most of the search queries are satisfied by the separate columns you will get pretty good performance.  If your query misses on the separate columns you can fall back to make a fulltext search on the long JSON string.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40220707
0
 

Author Comment

by:epifanio67
ID: 40220727
Thank you Ray,

Really appreciate the tips...

Regards,
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40220770
Glad to help.  If you need examples or have any questions about it, we will be here!
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question