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:

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

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:

$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>";

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?

Recommended Answers

All 9 Replies

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.

You definitely want to unserialize your data before you can use it:

<?php
$myData = unserialize($row);

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
echo "<td>Services:" . $row[services] . "</td>";
?>

So I tried doing something like this:

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

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.

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
echo "<td>Services:" . $row[services] . "</td>";
?>

So I tried doing something like this:

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

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);

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>';
?>

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.

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.

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.

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
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>";
?>

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

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

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.

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:

You da man! Right on. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.