PHP to grab a small piece of data from an array

Robert Granlund
Robert Granlund used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.

Author

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

Author

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
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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.

Author

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.

Author

Commented:
Is there any way to fix it? Like a tool I can run it through?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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 FavorFractional CTO
Distinguished Expert 2018

Commented:
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.

Author

Commented:
How do I find character 10284 ?  Is there an easy way?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.

Author

Commented:
Is there a way to query specific parts of the serialized data without grabbing the entire large chunk?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial