Help with unserialize array()

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Help with unserialize array()

Post by dv_evan »

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


Hello everyone,
I am have problem executing an output for an array from a database. I was succesful in unputting and serialize the data, however I am trying now for the longest while to unserialize the output, but without success. here is the code below, and the output is: Arraya:2:{i:0;sa:3:{i:0;sa:3:{i:0;sN;N;N;N;N;N;N;N;N;N;N;

Kindly help.
Dave

Code: Select all

 
<?
include ("Conn.php");
$query = "select * from Exhibitors";
$result=mysql_query($query);
 
$num=mysql_num_rows($result);
 
$i=0;
while ($i< $num){
$output1 =mysql_result($result,$i,"Exh_Type");
 
echo ($output1);
 
$i++;
}
?>

~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Help with unserialize array()

Post by aceconcepts »

Providing you serialized your data when inserting it to the database (it looks like you did), you should be able to simply unserialize() the output.
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Re: Help with unserialize array()

Post by dv_evan »

Thanks,
I tried using the serialize function, but do not know where exactly to tailored it into my codes. Can anyone, look at my code snippets and see where/how to use the unserialize function?

Dave
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Help with unserialize array()

Post by aceconcepts »

You're going to want to serialize your array before it is inserted into the database.

Code: Select all

 
$array_to_store=serialize($array_variable);
 
Whenever you want to retrieve your array you simply unserialize it:

Code: Select all

 
$myArray=unserialize($row['array_in_database']);
 
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Re: Help with unserialize array()

Post by dv_evan »

Thank you very very much, well appreciated.

Dave
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Re: Help with unserialize array()

Post by dv_evan »

Guys, I am really taking some blow on this , the unserialize is not working. I really need some help.
Just to recap quickly, I have inserted by checkbox serialized data, now I am trying to output by echo the data. But these codes are now working, as you will see in the codes below I am trying to get unserialize output in anyway possible:
Note:
(1)The table name as you can see from code is : Exhibitors
(2)The field name is: Exh_Type
(3)The checkbox input name= box[], and
(4)this is what shows up in the database: 23Arraya:2:{i:0;sa:3:{i:0;sa:3:{i:0;sN;N;N;N;N;N;N;N;N;N;N;N;N;N;


<?
include ("Conn.php");
$query = "select * from Exhibitors";
$result=mysql_query($query);


$num=mysql_num_rows($result);
$row=mysql_fetch_array($result);

echo ($num);

$i=0;
while ($i< $num){

// these are all the output I tried but nothing work

$output1 =mysql_result($result,$i,"Exh_Type");
$output2 = unserialize($row['box']);
$output3 = unserialize($row['Exh_Type']);
$output4 = unserialize($output1);
$output5 = unserialize($row[$output1]);

echo ($output1);
echo ($output2);
echo ($output3);
echo ($output4);
echo ($output5);

$i++;
}
?>


Dave
PHP newbie.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Help with unserialize array()

Post by John Cartwright »

Looks like the max length of the database column isn't long enough.
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Re: Help with unserialize array()

Post by dv_evan »

I am really puzzled and need advice what to do.

Dave
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Help with unserialize array()

Post by John Cartwright »

Jcart wrote:Looks like the max length of the database column isn't long enough.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Help with unserialize array()

Post by aceconcepts »

In your database you may need to change the "length" value of you field/column. e.g. field_1 varchar(100)
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Re: Help with unserialize array()

Post by dv_evan »

thanks Aceconcept & Jcart, However this did not solved the problem.

I change the lenght to varchar (100) in the database but still cannot get the output to unserialize.

Any other suggestions?

Dave.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Help with unserialize array()

Post by aceconcepts »

Try chanigng the data type to TEXT from VARCHAR - just as a test.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Help with unserialize array()

Post by John Cartwright »

What do the values look like in the database now?
dv_evan
Forum Commoner
Posts: 42
Joined: Wed Apr 09, 2008 8:23 am

Re: Help with unserialize array()

Post by dv_evan »

I changed to Text, but the output is still the same, a set of serialized data.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Help with unserialize array()

Post by aceconcepts »

Can you show your code?
Post Reply