benluke Posted May 24, 2006 Share Posted May 24, 2006 Hi,Im storing arrays in mysql and "trying" to unserialize the data. But im pretty much stuck! Can anybody help me?Im using 3 scripts/pages.form.php[code]<form action="submit.php" method="post"><input type="checkbox" name="knowledge1[]" value="A"> <input type="checkbox" name="knowledge1[]" value="B"> <input type="checkbox" name="knowledge1[]" value="C"> <input type="checkbox" name="knowledge1[]" value="D"> <br><input type="checkbox" name="knowledge2[]" value="A"> <input type="checkbox" name="knowledge2[]" value="B"> <input type="checkbox" name="knowledge2[]" value="C"><input type="checkbox" name="knowledge2[]" value="D"> <br><input type="checkbox" name="knowledge3[]" value="A"> <input type="checkbox" name="knowledge3[]" value="B"> <input type="checkbox" name="knowledge3[]" value="C"><input type="checkbox" name="knowledge3[]" value="D"> <input name="pc" type="hidden" value="pc"></form>[/code]submit.php[code]<?$pc1=serialize($_POST['knowledge1']);$pc2=serialize($_POST['knowledge2']); $pc3=serialize($_POST['knowledge3']);$update = $_POST["pc"];if ($update == 'pc'){ $query="UPDATE hsc SET knowledge_1 = '$pc1', knowledge_2 = '$pc2', knowledge_3 = '$pc3', submitted = '1', submitted_date=now() WHERE userid = $userid LIMIT 1"; $result=mysql_query($query); if ($result){ echo "<h3>Submitted</h3> <p>This has been successfully submitted.<br><br> <a href='index.php'>Click Here</a> to continue</p><br><br><br><br><br><br><br><br>"; } elseif (!$result){ echo "<h3>ERROR</h3><p> There was a Problem submitting .<br><br> If the problem persists please contact the support team."; }}?>[/code]The above 2 scripts seem to be working fine, the data is being stored in the database looking like this a:4:{i:0;s:1:"A";i:1;s:1:Im having problems with pulling the info and displaying it. Infact i cant work out how to do it? Im thinking this post should have gone in PHP newbie. Still, can anybody help. How do i pull the arrays from the DB and disp[ly them??Benluke Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/ Share on other sites More sharing options...
trq Posted May 24, 2006 Share Posted May 24, 2006 A quick example.[code]<?php // connect to db. $sql = "SELECT knowledge_1 FROM hsc WHERE userid = $userid LIMIT 1"; if ($result = mysql_query($sql)) { $row = mysql_fetch_assoc($result); $array = unserialize($row); print_r($array); }?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-38563 Share on other sites More sharing options...
benluke Posted May 24, 2006 Author Share Posted May 24, 2006 Thanks thorpe,Just tried your code. Not getting any errors but it does not display any data. Just blankChecked the database to make sure data is stored and thats fine a:4:{i:0;s:1:"A";i:1;s:1: is stored in the knowledge_1 fieldThanks benluke Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-38569 Share on other sites More sharing options...
benluke Posted May 24, 2006 Author Share Posted May 24, 2006 can Anybody help me out with this one??ben Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-38661 Share on other sites More sharing options...
benluke Posted May 25, 2006 Author Share Posted May 25, 2006 Could the problem be that it has not been stored in the table correctly??Im at a dead end with this at the mo.this is the data in the table[code]a:1:{i:0;s:1:"D";}[/code]this is the code that thorpe kindly supplied[code]<?php $sql = "SELECT knowledge_1 FROM hsc WHERE userid = $userid LIMIT 1"; if ($result = mysql_query($sql)) { $row = mysql_fetch_assoc($result); $array = unserialize($row); print_r($array); } else { echo "there was a problem"; }?>[/code]Its returning a blank screen. Could anyone shed any lite on this for me please??Benluke Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-38827 Share on other sites More sharing options...
Crimpage Posted May 25, 2006 Share Posted May 25, 2006 I didn't think you would be able to unserialize all of $row. I think you will need to do:$array = unserialize($row['knowledge_1']);Crimpage Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-38832 Share on other sites More sharing options...
benluke Posted May 25, 2006 Author Share Posted May 25, 2006 Hi Crimpage, Tahnks for your help with this.[code]<?php $sql = "SELECT knowledge_1 FROM hsc WHERE userid = $userid LIMIT 1"; if ($result = mysql_query($sql)) { $row = mysql_fetch_assoc($result); $array = unserialize($row['knowledge_1']); print_r($array); } else { echo "there was a problem"; }?>[/code]unfortunately im still getting same blank screen.benluke Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-38837 Share on other sites More sharing options...
Crimpage Posted May 25, 2006 Share Posted May 25, 2006 Right after $row = ...can you do print_r ($row);see what it returns, if nothing, then it is a problem with the sql query . $userid variable... Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-38839 Share on other sites More sharing options...
benluke Posted May 25, 2006 Author Share Posted May 25, 2006 hi,that does display the folllowing[code]Array ( [knowledge_1] => a:2:{i:0;s:1:"A";i:1;s:1: )[/code]ben Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-38858 Share on other sites More sharing options...
Crimpage Posted May 25, 2006 Share Posted May 25, 2006 try setting after $row = etc...$array = array();before running the $array = unserialize etc... Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-38870 Share on other sites More sharing options...
kenrbnsn Posted May 25, 2006 Share Posted May 25, 2006 First, go back to how you store the serialized data in your database and change it to use the mysql_real_escape_string() function:[code]<?php$pc1=mysql_real_escape_string(serialize($_POST['knowledge1']));$pc2=mysql_real_escape_string(serialize($_POST['knowledge2']));$pc3=mysql_real_escape_string(serialize($_POST['knowledge3']));?>[/code]This will make sure the data is properly escaped so it goes into the database correctly.Once the data is in the database correctly, then retrieving it should be easy.[code]<?php $sql = "SELECT knowledge_1 FROM hsc WHERE userid = $userid LIMIT 1"; $result = mysql_query($sql) or die("Problem with query: $sql<br>" . mysql_error()); $row = mysql_fetch_assoc($result); echo '<pre>' . print_r($row,true) . '</pre>'; $array = unserialize($row['knowledge_1']); echo '<pre>' . print_r($array,true) . '</pre>';?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-38886 Share on other sites More sharing options...
benluke Posted June 19, 2006 Author Share Posted June 19, 2006 Thanks to everyone,works perfectly nowbenluke Quote Link to comment https://forums.phpfreaks.com/topic/10348-unserialize-really-need-some-help-please/#findComment-47277 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.