unserialize an array! spitting out jibberish

Reply

Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

unserialize an array! spitting out jibberish

 
0
  #1
Sep 19th, 2006
Hey, I finally got my php code to accept an array into my database without it just putting "Array" in the database field, by using "serialize" like so:

[PHP]$services=serialize($_POST['services']);[/PHP]

So now it's putting jibberish in the database field like it's supposed to, but I'm unable to use "unserialize" to get it to read out the information correctly when called from the database. When retrieving it from the database I use:

[PHP]$result = mysql_query("SELECT * FROM billingrequest WHERE requestid='$request_id'");
while($row = mysql_fetch_array($result))
{
echo "<td width=\"183\" rowspan=\"2\"><center>" . $row['services'] . "</center></td>";[/PHP]

And I've tried sticking "unserialize in there in number of ways, but can't seem to get it to work. Anyone know how I'm supposed to do it?
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 8
Reputation: medora is an unknown quantity at this point 
Solved Threads: 1
medora medora is offline Offline
Newbie Poster

Re: unserialize an array! spitting out jibberish

 
0
  #2
Sep 19th, 2006
You definitely want to unserialize your data before you can use it:

<?php
$myData = unserialize($row['services']);

var_dump($myData); // see what's inside

?>

If you are dealing with array there - you probably want to loop over the array to extract the services.
Last edited by medora; Sep 19th, 2006 at 9:51 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: unserialize an array! spitting out jibberish

 
0
  #3
Sep 20th, 2006
Originally Posted by medora View Post
You definitely want to unserialize your data before you can use it:

<?php
$myData = unserialize($row['services']);

var_dump($myData); // see what's inside

?>

If you are dealing with array there - you probably want to loop over the array to extract the services.
Thanks for the reply. However I've already tried using it in that form. The problem is, because of the way I'm displaying it, it doesn't seem to be working. Basically, I've got it set up like this:

[PHP]<?php
echo "<td>Services:" . $row[services] . "</td>";
?>[/PHP]

So I tried doing something like this:

[PHP]<?php
$mydata = unserialize($row['services']);
echo "<td>Servicesmydata</td>";
?>[/PHP]

But to no avail. Still doesn't really work. But I'll try the var_dump to check it out. That's a good idea.
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 8
Reputation: medora is an unknown quantity at this point 
Solved Threads: 1
medora medora is offline Offline
Newbie Poster

Re: unserialize an array! spitting out jibberish

 
0
  #4
Sep 20th, 2006
Originally Posted by nathanpacker View Post
Thanks for the reply. However I've already tried using it in that form. The problem is, because of the way I'm displaying it, it doesn't seem to be working. Basically, I've got it set up like this:

[php]<?php
echo "<td>Services:" . $row[services] . "</td>";
?>[/php]
So I tried doing something like this:

[php]<?php
$mydata = unserialize($row['services']);
echo "<td>Servicesmydata</td>";
?>[/php]
But to no avail. Still doesn't really work. But I'll try the var_dump to check it out. That's a good idea.
Thanks.
Try this code:

<?php
$mydata = unserialize($row['services']);

if (is_array($mydata) == false) {
echo 'It is not an array!';
exit;
}

$itemcount = count($mydata);

if ($itemcount == 0) {
echo 'Array is empty!';
}

echo '<td>';
echo 'Services: ';
for ($i=0;$i<$itemcount;$i++) {
echo $mydata[$i] . ' ';
}
echo '</td>';
?>
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: unserialize an array! spitting out jibberish

 
0
  #5
Sep 20th, 2006
I'll try that. Thanks. Oh, just fyi, when I tried doing it like I replied in my last post, instead of displaying all the jibbering data, it just went back to displaying "Array". That's what I was getting before I serialized it. Anyway, I'll try what you suggested and get back to you.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: unserialize an array! spitting out jibberish

 
0
  #6
Sep 20th, 2006
Hey, that worked! Just one more problem now. How do I further format the output? It's only submitting the first word of the option. I have put quotes around the value in the code, so that it would take the whole thing, but it's only taking the first word. Like if the option is "Backup Services", it's only outputting "Backup."

Edit: I don't know if this is related or not, but I just noticed another problem. Sometimes when you select more than two options, it's failing and saying it's not an array. I sort of understand the code you gave me, but not enough to figure out why it's giving me this problem. I'll try to explain it exactly so you can have a good picture what's happening.

Here are the options:

Monitored
Network/Application Support
License Management
Backup Server
Backup Agent


If for instance you choose "Monitored" and "Backup Agent" it works. But if you choose "Monitored" and "Network/Application Support" then it doesn't work. Similarly, if you choose "Backup Agent" and "Backup Server" it works, but not if you choose "Network/Application Support" and "License Management". If you choose just one of any of the options, it works. Totally crazy! Can't figure it out. I'll look more closely and see if I can find anything, but I figured you might see it easier than me.
Thanks.
Last edited by nathanpacker; Sep 20th, 2006 at 5:53 am.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: unserialize an array! spitting out jibberish

 
0
  #7
Sep 20th, 2006
Ok, I have to apologize, but this is crazy! As I wrote my last post, it worked just as I said it did. I haven't made any changes, but now, it won't work at all if you select more than one option. Crazy.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: unserialize an array! spitting out jibberish

 
0
  #8
Sep 20th, 2006
Now I don't think the problem is in that code. I think that code is working fine. But I think the code that is actually submitting the info to the database is doing something wrong, making it not an array. Basically, the form is like this:

[PHP]<?php
echo "<form method=post action=myform.php>";
echo "Services:<br>";
echo "<input type=checkbox name=services[] value=\"Monitored\">";
echo "Monitored:<br>";
echo "<input type=checkbox name=services[] value=\"Network/Application Support\">";
echo "Network/Application Support:<br>";
echo "<input type=checkbox name=services[] value=\"License Management\">";
echo "License Management <br>";
echo "<input type=checkbox name=services[] value=\"Backup Server\">";
echo "Backup Server <br>";
echo "<input type=checkbox name=services[] value=\"Backup Agent\">";
echo "Backup Agent <br>";
echo "</form>";
?>[/PHP]

So it submits to myform.php or whatever. Then myform.php submits the information to the database like so:

[PHP]<?php
$services=serialize($_POST['services']);
$sql="INSERT INTO billingrequest (services)
VALUES ('$services')";
?>[/PHP]

And then like I said, it looks like it's working if you only choose one option, and it still calls it an array, but if you choose more than one option, it doesn't output an array. I know it's getting information to the database, because I can look at the actual table and see the info. I'm so new at this I'm just all confused. Thanks for your help.
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: nathanpacker is an unknown quantity at this point 
Solved Threads: 0
nathanpacker's Avatar
nathanpacker nathanpacker is offline Offline
Posting Whiz in Training

Re: unserialize an array! spitting out jibberish

 
0
  #9
Sep 21st, 2006
Problem solved! I realized that I had taken the default datatype for that field in mysql. So I was using VARCHAR(45)! The field was only accepting 45 characters! That's why it was only working when you chose one box. Problem fixed. :mrgreen: :cheesy: :mrgreen: :cheesy:
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 8
Reputation: medora is an unknown quantity at this point 
Solved Threads: 1
medora medora is offline Offline
Newbie Poster

Re: unserialize an array! spitting out jibberish

 
0
  #10
Sep 21st, 2006
You da man! Right on.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC