Forum Moderators: coopster

Message Too Old, No Replies

Unserialize array

         

Ann_G

9:45 pm on Oct 6, 2004 (gmt 0)

10+ Year Member



Hi:

I got some good help in using the serialize function on an array I needed stored in a table. I thought it would be a breeze to unserialize, but I must be missing something since I can't get it to work.

This is how the array is serialized:
// Make the array

if(is_array($_POST['inc_type'])){
$postinc_typeserialized = serialize($_POST['inc_type']);
}else {
$inc_type = '';}

//and then I insert it into my table

query = "insert into table(inc_type)"
Values
('".mysql_escape_string($postinc_typeserialized)."', ...

This works fine but when I do the SELECT to get the array from the table it comes out with the unserialized stuff in front of the term,
for example: a:1:{i:0;s:5:"theft";}

This is one of my attempts at unserializing:
$postinc_typeunserialized = unserialize($inc_type[inc_type]);
$inc_type = stripslashes($row['6']);

Any suggestions what I'm doing wrong? Should I use a foreach loop to go through each item in the array?
As you can tell I'm not too good at PHP.

Thanks in advance.

mincklerstraat

8:26 am on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could try stripping slashes (if you have magic quotes gpc on, as a lot do without knowing it, this could be screwing things up with quotes).

if(is_array($_POST['inc_type'])){
if(get_magic_quotes_gpc()) array_map('stripslashes', $_POST['inc_type']);
$postinc_typeserialized = serialize($_POST['inc_type']);
}else {
$inc_type = '';}

To debug, try outputting this value, and unserializing it before it goes into the database, then you can get a better idea of what's happening.

Ann_G

5:07 pm on Oct 7, 2004 (gmt 0)

10+ Year Member



Thanks for trying to help butit still doesn't work.

mincklerstraat

5:29 pm on Oct 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, looking at your code again. Don't know, this may be completely off, but if you're fairly new, it might help:

this part:
$postinc_typeunserialized = unserialize($inc_type[inc_type]);
$inc_type = stripslashes($row['6']);

you have $postinc_typeunserialized which should be the array created by unserializing - you know it's here you should be trying to find your data again that was unserialized? try echo '<pre>'.print_r($postinc_typeunserialized).'</pre>';
or if this format confuses you,
foreach($postinc_typeunserialized as $k => $v){
echo '<br />$postinc_typeunserialized['.htmlspecialchars($k).'] == '.htmlspecialchars($v);
}

My guess is that you might want to do a bit of a review of arrays in general, maybe even mysql queries / results - the code you posted doesn't make much sense to me, I think it's likely that you're trying to do something else, but don't have enough code here for me to tell.

Ann_G

8:24 pm on Oct 7, 2004 (gmt 0)

10+ Year Member



I appreciate you trying to help, but the debugging doesn't work, the echo <pre>... just gives me a number and the foreach a warning saying not a valid MYSQL statement or something similar.

This is the array part of my form:
<select>Please select a type of incident:
<select name="inc_type[]" multiple>
<option value="disruptive" SELECTED>Disruptive</option>
<option value="accident">Accident</option>
<option value="theft">Theft</option>
<option value="other">Other</option>
</select>

And this is the array validated and serialized:
if(is_array($_POST['inc_type'])){
if(get_magic_quotes_gpc()) array_map('stripslashes', $_POST['inc_type']);
$postinc_typeserialized = serialize($_POST['inc_type']);
}else {
$inc_type = '';}

After I insert it into my table:
$query = "INSERT INTO incidents (inc_type) VALUES ('".mysql_escape_string($postinc_typeserialized)."',)";
$result = @mysql_query ($query);

It looks like this: a:1:{i:0;s:5:"theft";}

Then I do:
$query = "SELECT * FROM incidents
$result = mysql_query ($query);

while ($row = mysql_fetch_array($result)) {
$unserialpostinc_typeserialized = unserializ($inc_type['inc_type']);
$inc_type = stripslashes($row['6']);

$display_block .= "<p>
<strong>Type:</strong>$inc_type <br /></p>;
}
?>

I cut the script to just this array part, which is the only part that is not working with my otherwise rather long form. If you have any other suggestions --great, otherwise I'm just going to have to live with those extra characters in fron to the word. I already appreciate you taking a look in the first place so don't feel bad if no solution can be found.

mincklerstraat

7:44 am on Oct 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'd thought there might be a problem like this one somewhere in the mix. Part of it is syntax in using mysql with php.

Your code:
while ($row = mysql_fetch_array($result)) {
$unserialpostinc_typeserialized = unserializ($inc_type['inc_type']);
$inc_type = stripslashes($row['6']);

this sets $row to what's 'gotten' from fetching the info from your $result, but then it's not $row that you're unserializing, it's this thing $inc_type['inc_type']. What you need is more like:

while ($row = mysql_fetch_array($result)) {
$unserialpostinc_typeserialized = unserialize($row['inc_type']);

here you are unserializing what needs to be unserialized - the stuff you're getting out of the database. Probably you should put error_reporting('E_ALL'); at the top of your code during development - if you had this there this time, you would have probably gotten an error that this array element hadn't been set yet, which can be useful information!

then going further in your code:

$display_block .= "<p>
<strong>Type:</strong>$inc_type <br /></p>;
}
in your code, you've set this to $row['6']; I'm guessing, though, that what you want is something from the array you just unserialized, since why else would you be so bothered by all this? So here we'll insert some of the debugging code I posted above, except modifying it for the correct variables:

foreach($unserialpostinc_typeserialized as $k => $v){
echo '<br />$unserialpostinc_typeserialized['.htmlspecialchars($k).'] == '.htmlspecialchars($v);
} // close
foreach($unserialpostinc_typeserialized
} // close while ($row =

you can then select which element of this array you want to output by .= to your variable $display_block .

Ann_G

5:55 pm on Oct 10, 2004 (gmt 0)

10+ Year Member



I apologize for not answering sooner. My life got unusually busy all of a sudden and I didn't have a chance to get back to you until now. I haven't had a chance to try what you suggested but I'll let you know as soon as I have.

I want you to know I sincerely appreciate your help and you taking the time to respond.

Ann_G

9:48 pm on Oct 11, 2004 (gmt 0)

10+ Year Member



Today I followed your suggestions and after a little bit of stumbling, because I'm rather new at PHP, it worked. I now get whatever term the user selects from my list without any of that unserialized stuff in front.

I'm tremendously grateful for your help. You cured my headache.