Forum Moderators: coopster
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.
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.
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.
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.
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 .
I want you to know I sincerely appreciate your help and you taking the time to respond.