I only want to show the email. also, is there a way to show everything but the email?
PHPSQL
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 arrayecho $info['ii_email'];
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;}
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).
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.
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.
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.
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.