By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,686 Members | 2,770 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,686 IT Pros & Developers. It's quick & easy.

Viewing checkbox data with MySQL and PHP

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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.