Page 1 of 1

Array Serialization and Unserialization in PHP and MySQL

Posted: Thu May 28, 2009 5:18 pm
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.';
}
?>

Re: Array Serialization and Unserialization in PHP and MySQL

Posted: Fri May 29, 2009 2:09 am
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 ;)

Re: Array Serialization and Unserialization in PHP and MySQL

Posted: Fri May 29, 2009 9:23 am
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:

Re: Array Serialization and Unserialization in PHP and MySQL

Posted: Fri May 29, 2009 9:41 am
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 ;)

Re: Array Serialization and Unserialization in PHP and MySQL

Posted: Fri May 29, 2009 9:59 am
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?

Re: Array Serialization and Unserialization in PHP and MySQL

Posted: Fri May 29, 2009 10:07 am
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.

Re: Array Serialization and Unserialization in PHP and MySQL

Posted: Fri May 29, 2009 10:16 am
by lmg
I was hoping that wouldn't be the answer :(

Thanks for all of your help!