PHP to grab a small piece of data from an array

PHP Query returns an array and in it is serialized data.  I need to just grab a small piece of data from the array.  I need to grab: ii_email

Array ( [data] => a:86:{s:9:"ii_seller";s:0:"";s:2:"id";s:3:"117";s:12:"ii_firstname";s:13:"Evander Fogle";s:10:"ii_address";s:59:" 5671 STREETAtlanta, GA 30342";s:8:"ii_email";s:26:"MY-EMAIL.COM";s:8:"ii_phone";s:12:"404-444-4444";s:10:"ii_pi_name";a:1:{i:0;s:0:"";}s:11:"ii_pi_phone";a:1:{i:0;s:0:"";}s:11:"ii_pi_email";a:1:{i:0;s:0:"";}s:11:"ii_pi_name2";s:0:"";s:12:"ii_pi_phone2";s:0:"";s:12:"ii_pi_email2";s:0:"";s:11:"ii_pi_name3";s:0:"";s:12:"ii_pi_phone3";s:0:"";s:12:"ii_pi_email3";s:0:"";s:15:"oi_hospitallist";a:4:{i:0;s:38:"Total Joint Replacement Prehab / Rehab";i:1;s:30:"Sports Medicine Prehab / Rehab";i:3;s:38:"Non-Surgical Orthopedic Prehab / Rehab";i:5;s:5:"Other";}

Open in new window


Here is my Query:
<?php
        $query = "SELECT data FROM `data` LIMIT 1"; 
      $result = mysqli_query($con, $query);  

      while($row = $result->fetch_assoc()) {
  $data = print_r($row, true);
echo $data;
   
}

Open in new window


I only want to show the email.  also, is there a way to show everything but the email?
LVL 8
rgranlundAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Sure. You need to use the unserialize() command to turn your data into an array. You can then access the elements of the array in the normal PHP way:

$query = "SELECT data FROM `data` LIMIT 1"; 
$result = mysqli_query($con, $query);  

$row = $result->fetch_assoc(); // no need to loop for a single record!
$info = unserialize($row['data']); // convert to serialized string into an array

echo $info['ii_email'];

Open in new window

If you want to show everything but the email, you could just remove the email from the array:

unset($info['ii_email']);

Now the $info will contain everything except the email.
rgranlundAuthor Commented:
When I do it that way I get:
Notice: unserialize(): Error at offset 10284 of 83494 bytes in /home/rom32825/public_html/dev/download.php on line 38
Line 39 is:
$info = unserialize($row['data']); // convert to serialized string into an array
rgranlundAuthor Commented:
I have more than one record to grab but was just printing out one for testing.  My Code:
        $query = "SELECT data FROM `data`"; 
      $result = mysqli_query($con, $query);  
      while($row = $result->fetch_assoc()) {
       $info = unserialize($row['data']);

echo $info['ii_address'];

//echo $data;
   
}

Open in new window

This line:
 $info = unserialize($row['data']);

Gives the error:
Notice: unserialize(): Error at offset 10284 of 83494 bytes in /home/rom32825/public_html/dev/download.php on line 36
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

Chris StanyonWebDevCommented:
OK. That error implies that your serialized data in the DB is flawed somehow. This can sometime happen if the serialized string is somehow edited directly.

It also look like the data stored in the serialized data is 83kb in size !!! Are you sure that's correct, because that doesn't remotely match your original post. That's quite a massive string to serialized into your DB.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rgranlundAuthor Commented:
I did not post the entire string.  It is VERY large.  I'm not sure why they built it that way.  I'm going to go in and add fields for everything instead of keeping it this way.
rgranlundAuthor Commented:
Is there any way to fix it? Like a tool I can run it through?
Chris StanyonWebDevCommented:
I'm not aware of any tool that can fix it automatically for you. You could load the string into a decent text editor and have a look at character 10284 to see if the problem is apparent.

The way a serialized string works is based on field lengths and content so you may have something like:

s:5:"Value";s:3:"abc";

That means you have a string with 5 character (Value) and a string with 3 character (abc). If you end up with something like s:5:"val", then you have a problem - the code is expecting 5 characters and you only have 3 (val).
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Typically Error at offset means someone has incorrectly attempted to modify a database using a tool like myPHPAdmin which has no serialization awareness. Or possibly the mysql command line tool.

Note:  You should only use a tool like wp-cli (serialized data aware) to edit serialized data.

If one problem exists, potentially many problem exist.

You can...

1) Dump your database using...

2) Use the https://github.com/Blogestudio/Fix-Serialization to fix all problems.

3) Drop + Recreate database.

4) Reload database with the mysql command line tool.

Tip: You can use the above script on parts of databases too + this can quickly become very complex.

If you only have one row in a database to fix, you can likely eyeball the record + fix it with phpMyAdmin.

If you have many records to fix, hire someone to help you, as this can turn into a large amount of time + fix is best done precisely looking at the entire database.
rgranlundAuthor Commented:
How do I find character 10284 ?  Is there an easy way?
Chris StanyonWebDevCommented:
If you paste the entire string into something like notepad++, it will show you various stats in the bottom of the screen, including the position of the cursor on a given line. Use can use that to locate the correct place.
rgranlundAuthor Commented:
Is there a way to query specific parts of the serialized data without grabbing the entire large chunk?
Chris StanyonWebDevCommented:
Nope - not that I'm aware of. One of the problems with serialized data is that it's all or nothing. One wrong byte and your entire dataset can become 'corrupt'.

You could potentially build a Regular Expression to extract some of the data, but it's always going to be messy, and potentially unreliable. As soon as your app starts to rely on hacks to recover some kind of data integrity, you're asking for trouble.

The only real solution is to try and fix the data, otherwise the whole thing becomes redundant. Any query against your data will fail. No app can read it properly.

You could try the script that David mentioned. It's aimed at fixing the length attributes, so if the problem lies elsewhere, it won't help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.