Array Serialization and Unserialization in PHP and MySQL

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Array Serialization and Unserialization in PHP and MySQL

Post by lmg »

I have some data which is in an array to be serialized going into a database and then I want to retrieve the information and display it. I am working an example found on this site.

You are given a set of four checkboxes which you click and press submit. The information is supposed to be written to the database (which it does) and then retrieved and printed to the screen. The problem I appear to be having has to do with the unserialize() function.

Here is the output I am getting:
doQuery = Resource id #6...Array.........

Warning: Invalid argument supplied for foreach() in /home/a5317204/public_html/myTestDir/colorTest.php on line 28

...Array.........

Warning: Invalid argument supplied for foreach() in /home/a5317204/public_html/myTestDir/colorTest.php on line 28

...Array.........

Warning: Invalid argument supplied for foreach() in /home/a5317204/public_html/myTestDir/colorTest.php on line 28

...Array.........

Warning: Invalid argument supplied for foreach() in /home/a5317204/public_html/myTestDir/colorTest.php on line 28
and here is my code:

html

Code: Select all

<form method="post" action="colorTest.php">
<input type="checkbox" id="colors[]" value="red" /> Red
<input type="checkbox" id="colors[]" value="blue" /> Blue
<input type="checkbox" id="colors[]" value="green" /> Green
<input type="checkbox" id="colors[]" value="yellow" /> Yellow
<input type="submit">
</form>
php

Code: Select all

<?php
 
$username="user";
$password="pass";
$database="dbt";
 
@mysql_connect(localhost,$username,$password) or die("Unable to connect.");
@mysql_select_db($database) or die( "Unable to select database.");
 
$colors=serialize($_POST['colors']); //takes the data from a post operation...
$query="INSERT INTO color VALUES('$colors')"; 
$doQuery=mysql_query($query);
 
 
$query="SELECT * FROM color";
$doQuery=mysql_query($query);
print "doQuery = ".$doQuery;
if($doQuery != null)
    $numrows=mysql_num_rows($doQuery);
    
if($numrows>0)
{
 while($colors=mysql_fetch_array($doQuery))
  {
  print "...".$colors."...";
  $colors=unserialize($colors["color"]);
    print "...".$colors."...";
  foreach($colors as $shirt)
   {
      print $shirt.', ';
   }
  }
}
else
{
 print 'No colors in database.';
}
?>
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Array Serialization and Unserialization in PHP and MySQL

Post by VladSun »

Change your code like this:

Code: Select all

while($colors=mysql_fetch_array($doQuery))
  {
  print "Serialized color string: ".$colors."<br />";
  $colors=unserialize($colors["color"]);
  print "Unserialized color array: ".$colors."<br />";
  foreach($colors as $shirt)
   {
      print $shirt.', ';
   }
  }
and you will see where your error is ;)
There are 10 types of people in this world, those who understand binary and those who don't
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: Array Serialization and Unserialization in PHP and MySQL

Post by lmg »

Thanks for the reply.

I know where the error is, but I am not sure WHY it is doing this. I think I am missing some key concept here :cry:
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Array Serialization and Unserialization in PHP and MySQL

Post by VladSun »

I'm sorry, but the example page you are using is wrong

It can't be :

Code: Select all

<input type="checkbox" id="colors[]" value="red" /> Red    <input type="checkbox" id="colors[]" value="blue" /> Blue    <input type="checkbox" id="colors[]" value="green" /> Green    <input type="checkbox" id="colors[]" value="yellow" /> Yellow
It must be:

Code: Select all

<input type="checkbox" name="colors[]" value="red" /> Red    <input type="checkbox" name="colors[]" value="blue" /> Blue    <input type="checkbox" name="colors[]" value="green" /> Green    <input type="checkbox" name="colors[]" value="yellow" /> Yellow
Then ... create a simple PHP file like this:

Code: Select all

<form method="post" action="">
    <input type="checkbox" name="colors[]" value="red" /> Red
    <input type="checkbox" name="colors[]" value="blue" /> Blue
    <input type="checkbox" name="colors[]" value="green" /> Green
    <input type="checkbox" name="colors[]" value="yellow" /> Yellow
    <input type="submit">
</form>
 
<?php
 
print_r($_POST);
Check some colors and submit it, then DON'T check any color and submit it again. I'm sure you'll see the difference ;)
There are 10 types of people in this world, those who understand binary and those who don't
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: Array Serialization and Unserialization in PHP and MySQL

Post by lmg »

Hooray it worked :D

Now that I have that working, what is a good way to write the array to a database and the retrieve it?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Array Serialization and Unserialization in PHP and MySQL

Post by VladSun »

The quick answer is - simply don't put serialized data into DB. There might be some cases where it could be a better way to do it, but in most cases you'll have to create additional tables and put separate record for every array item in them.
There are 10 types of people in this world, those who understand binary and those who don't
lmg
Forum Commoner
Posts: 34
Joined: Tue May 26, 2009 10:11 am

Re: Array Serialization and Unserialization in PHP and MySQL

Post by lmg »

I was hoping that wouldn't be the answer :(

Thanks for all of your help!
Post Reply