Page 1 of 1

[solved] database / serialize / unserialize / arrays... WHOA

Posted: Thu Oct 20, 2005 5:09 pm
by dallasx
Hey everyone

I'm new to the forum. I have tried everything that I know to solve this problem.

I'm making a clothing catalog. The problem I'm having is when I serialize an array of shirt sizes and quantities, I can't get a clean look at it. There's got to be something wrong with the following code or my logic.

Code: Select all

//this is what the serialized array looks like when echoed
//a:7:{s:2:"xs";i:30;s:2:"sm";i:31;s:2:"md";i:32;s:2:"lg";i:33;s:2:"xl";i:32;s:3:"xxl";i:31;s:4:"xxxl";i:30;}

$result = mssql_query("SELECT array FROM arrays WHERE id='8'");

$serialarray = mssql_fetch_array($result);

echo "ATTEMPT TO TRAVERSE THE SERIALIZED ARRAY<br>";

$a = $serialarray;

$a = unserialize($a);

// Key = shirt size, Value = quantity

while (list($key, $value) = each($a))
{
	echo "Key: ". $key ." > Value: ".$value."<br>";
}
// should print out this:
//Key: xs > Value: 30
//Key: sm > Value: 31
//Key: md > Value: 32
//Key: lg > Value: 33
//Key: xl > Value: 32
//Key: xxl > Value: 31
//Key: xxxl > Value: 30

Now that I think about it...

Posted: Fri Oct 21, 2005 2:10 pm
by dallasx
After looking at the above code, i think when the array is pulled from the db, the craziness begins there. The values before the array was stored in the database are set. I insert it into the database then pull it out and array values are lost.

Posted: Fri Oct 21, 2005 3:21 pm
by feyd
var_dump($serialarray) .. see what comes up..

Posted: Fri Oct 21, 2005 3:33 pm
by dallasx
feyd wrote:var_dump($serialarray) .. see what comes up..
Ok, I'll try that right now.

Posted: Fri Oct 21, 2005 3:51 pm
by dallasx
feyd wrote:var_dump($serialarray) .. see what comes up..
First... ignore the code above. I started a new file.
Ok... before I serialize it and insert it into the database, I get this:

Code: Select all

$mens_sizes["Small"]	= 10;
	$mens_sizes["Medium"]	= 15;
	$mens_sizes["Large"]	= 20;

	var_dump($mens_sizes);
OUTPUT:
array(3) { ["Small"]=> int(10) ["Medium"]=> int(15) ["Large"]=> int(20) }



Then, after I retrieve the serialized array from the db

Code: Select all

var_dump($dbarray);
OUTPUT:
array(1) { ["array"]=> string(58) "a:3:{s:5:"Small";i:10;s:6:"Medium";i:15;s:5:"Large";i:20;}" }




when I unserialize it...

Code: Select all

$dbarray = unserialize($dbarray);
var_dump($dbarray);
OUTPUT:
bool(false)

Posted: Fri Oct 21, 2005 3:57 pm
by feyd
I'm going to guess you don't understand what the problem is after that illustration?

Posted: Fri Oct 21, 2005 4:00 pm
by dallasx
feyd wrote:I'm going to guess you don't understand what the problem is after that illustration?
Heh... I have no clue.

Posted: Fri Oct 21, 2005 4:02 pm
by feyd
mssql_fetch_array() returns an array of all the fields you select, whether one or two hundred.

Code: Select all

$a = $serialarray['array'];

Posted: Fri Oct 21, 2005 4:10 pm
by dallasx
feyd wrote:mssql_fetch_array() returns an array of all the fields you select, whether one or two hundred.

Code: Select all

$a = $serialarray['array'];
Nice... it obviously worked...

The way I see it is that in the fetched array, the array I want is actually stored as a key and the data I want is stored as a string value?

Posted: Fri Oct 21, 2005 9:23 pm
by feyd
yep.

Good to know

Posted: Fri Oct 21, 2005 9:25 pm
by dallasx
Well I'll be a monkey's uncle...