| | |
unserialize an array! spitting out jibberish
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
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?
[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?
•
•
Join Date: Sep 2006
Posts: 8
Reputation:
Solved Threads: 1
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.
<?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.
•
•
•
•
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.
[PHP]<?php
echo "<td>Services:" . $row[services] . "</td>";
?>[/PHP]
So I tried doing something like this:
[PHP]<?php
$mydata = unserialize($row['services']);
echo "<td>Services
mydata</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.
•
•
Join Date: Sep 2006
Posts: 8
Reputation:
Solved Threads: 1
•
•
•
•
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.
<?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>';
?>
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.
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.
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.
[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.
![]() |
Other Threads in the PHP Forum
- Previous Thread: Preview Upload Image Problem
- Next Thread: bringing data from mysql using select box
| Thread Tools | Search this Thread |
ajax apache api array basics beginner binary broken cakephp checkbox class cms code codingproblem combobox cron curl database date display domain dynamic echo email error file files folder form format forms function functions google href htaccess html image include insert interactive ip java javascript joomla js limit link load login mail malfunctioning menu mlm mobile multiple mysql nodes oop outofmemmory paging parse paypal pdf php problem procedure query radio ram random recursion reference remote return script search server sessions sms source space sql syntax system table tutorial unset up-to-date update upload url validation validator variable video web webapplications websitecontactform youtube





