Avatar of Robert Granlund
Robert Granlund
Flag for United States of America asked on

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?
PHPSQL

Avatar of undefined
Last Comment
Chris Stanyon

8/22/2022 - Mon
Chris Stanyon

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.
Robert Granlund

ASKER
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
Robert Granlund

ASKER
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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
ASKER CERTIFIED SOLUTION
Chris Stanyon

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Robert Granlund

ASKER
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.
Robert Granlund

ASKER
Is there any way to fix it? Like a tool I can run it through?
Chris Stanyon

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).
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
David Favor

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.
Robert Granlund

ASKER
How do I find character 10284 ?  Is there an easy way?
Chris Stanyon

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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
Robert Granlund

ASKER
Is there a way to query specific parts of the serialized data without grabbing the entire large chunk?
Chris Stanyon

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.