471,090 Members | 1,361 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,090 software developers and data experts.

Viewing checkbox data with MySQL and PHP

Bee
Hi everyone,

I'm having trouble accessing a Mysql database containing checkbox data
that is posted from a form. I use the serialize function to store the
checkbox values in an array (in a column called "CHECKBOX").

Each row in the table has this format:

a:2:{i:0;s:7:"Peanuts";i:1;s:9:"Cows_milk";}

So I know the serialize function works fine. However, when using the
unserialize function to convert the data back into its single form. I
have no luck. The output for each row is "Array". Below is the code.
<?php //Code to connect to the database server
...
...

$query = "SELECT * FROM checkbox";
$doQuery = mysql_query($query);
$numrows = mysql_num_rows($doQuery);
if($numrows > 0)
{
while($checkbox = mysql_fetch_array($doQuery))
{
$check = unserialize($checkbox['CHECKBOX']);
echo($check);
}
}
?>

Can anyone offer any ideas about what I'm doing wrong?

Thanks in advance!

Bee.
Jul 20 '05 #1
4 2932
Bee wrote:
Can anyone offer any ideas about what I'm doing wrong?


Try:
echo $checkbox['CHECKBOX'];

If that prints the string that you have in the database, you have
problem in php side, if you prints something else, you have problem on
sql side.
Jul 20 '05 #2
Bee
> Try:
echo $checkbox['CHECKBOX'];

If that prints the string that you have in the database, you have
problem in php side, if you prints something else, you have problem on
sql side.


Thanks for replying.

I did try this and it printed out the strings in my database. So
therefore its a problem on the PHP side, as you commented. The thing
is, I cannot find out what the problem is i.e. why the output of each
string in the database is "Array" when I process the following:

$check = unserialize($checkbox['CHECKBOX']);
//code to loop array...
echo $check;

Any ideas are very welcome...(please!)

TIA.

Bee.
Jul 20 '05 #3
Hi Bee

Try using print_r($check) to view the contents of the $check array or try:

foreach ($check as $value) {
echo $value;
}

If the output of echo $value is still an array, loop through the $value
array as well.

foreach ($check as $value) {
foreach ($value as $innervalue) {
echo $innervalue;
}
}

--
Paul Barfoot
"Bee" <na******@gmail.com> wrote in message
news:62*************************@posting.google.co m...
Try:
echo $checkbox['CHECKBOX'];

If that prints the string that you have in the database, you have
problem in php side, if you prints something else, you have problem on
sql side.


Thanks for replying.

I did try this and it printed out the strings in my database. So
therefore its a problem on the PHP side, as you commented. The thing
is, I cannot find out what the problem is i.e. why the output of each
string in the database is "Array" when I process the following:

$check = unserialize($checkbox['CHECKBOX']);
//code to loop array...
echo $check;

Any ideas are very welcome...(please!)

TIA.

Bee.

Jul 20 '05 #4
Bee
Thanks very much! Now it works fine!

I guess I should've thought about looping through the actual array
itself. I ignored that each value returned an array, and expected
unserialize to do it all.

Cheers!

Bee.
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by RelaxoRy | last post: by
2 posts views Thread by info | last post: by
3 posts views Thread by elyob | last post: by
6 posts views Thread by Daz | last post: by
29 posts views Thread by Amer Neely | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.