Page 1 of 2
Help with unserialize array()
Posted: Tue Apr 22, 2008 8:16 pm
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:
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:
Posting Code in the Forums to learn how to do it too.
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 3:11 am
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.
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 6:41 am
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
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 7:14 am
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']);
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 7:40 am
by dv_evan
Thank you very very much, well appreciated.
Dave
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 9:13 am
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.
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 9:17 am
by John Cartwright
Looks like the max length of the database column isn't long enough.
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 9:42 am
by dv_evan
I am really puzzled and need advice what to do.
Dave
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 9:46 am
by John Cartwright
Jcart wrote:Looks like the max length of the database column isn't long enough.
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 9:58 am
by aceconcepts
In your database you may need to change the "length" value of you field/column. e.g. field_1 varchar(100)
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 10:44 am
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.
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 10:49 am
by aceconcepts
Try chanigng the data type to TEXT from VARCHAR - just as a test.
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 11:01 am
by John Cartwright
What do the values look like in the database now?
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 11:02 am
by dv_evan
I changed to Text, but the output is still the same, a set of serialized data.
Re: Help with unserialize array()
Posted: Wed Apr 23, 2008 11:10 am
by aceconcepts
Can you show your code?