Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

unserialize() arrays stored in mysql, help!!!

Status
Not open for further replies.

sophielois1

Technical User
May 2, 2006
16
GB
Hi been handed a database that has a number of arrays which have been stored using the serialize() function.

As i have never stored arrays in databases before, i am having a bit of problem pulling the info and displaying it.

Would somebody be kind enough to help me.

This is what i have.

table called bank
columns know1, know2, know3, know4,

from what i can gather these are the lines of code that origannlly stored the data
Code:
<?
$pc1=serialize($_POST['knowledge1']);
$pc2=serialize($_POST['knowledge2']); 
$pc3=serialize($_POST['knowledge3']); 
$pc4=serialize($_POST['knowledge4']);
?>
$query="UPDATE bank SET know1 = '$pc1', know2 = '$pc2', know3 = '$pc3', know4 = '$pc4', submitted_date=now()   WHERE userid = $userid LIMIT 1";

I am aware that i have to use the unserialize() function, but am struggling to get it working or really know how to implement it.

i have been trying to do somethinhg like this
Code:
$result = mysql_query("SELECT * FROM bank WHERE userid = '$userid'");

	while($row = mysql_fetch_object($result))
		{
		
		$pc=unserialize($pc1['knowledge1']);
		
		foreach($pc as $result)
 {
   echo "$result</br>";
 }

mmmmmmmm

really appreciate anybodies help

thanx Soph
 
Unless in the original script $_POST['knowledge1'], $_POST['knowledge2'], $_POST['knowledge3'] or $_POST['knowledge4'] were any arrays, no arrays were serialized. All that was serialized were four elements of a single array.

Those four elements were stored in the four columns of one row of a table. Those columns were named "know1", "know2", "know3" and "know4".

Your script fetches the data into an object (with mysql_fetch_object()), but then tries to unserialize the data by array references (with $pc1['knowledge1']). You need to make these to agree, and I recommend that you use mysql_fetch_assoc() instead of mysql_fetch_object().

Your script only attempts to unserialize() one column from the row, when 4 were stored, and refers to that column by the wrong name "knowledge1" instead of the correct "know1".





Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi sleipnir214,

thanks for the reply;

the $_POST['knowledge1'] came from a form

<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" />

<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" />

etc....

which was then stored to the db as shown above
Code:
<?
$pc1=serialize($_POST['knowledge1']);
$pc2=serialize($_POST['knowledge2']);
$pc3=serialize($_POST['knowledge3']);
$pc4=serialize($_POST['knowledge4']);
?>
$query="UPDATE bank SET know1 = '$pc1', know2 = '$pc2', know3 = '$pc3', know4 = '$pc4', submitted_date=now()   WHERE userid = $userid LIMIT 1";

"Your script only attempts to unserialize() one column from the row, when 4 were stored, and refers to that column by the wrong name "knowledge1" instead of the correct "know1"."

Your right it does

Code:
$result = mysql_query("SELECT * FROM bank WHERE userid = '$userid'");

    while($row = mysql_fetch_array($result))
        {
        
        $pc=unserialize($pc1['know1']);
        
        foreach($pc as $result)
 {
   echo "$result</br>";
 }

so bare with me. I'm learning fast.

So how would i pull all arrays?

Thanks Soph
 
You would pull the other three arrays the same way you do the first. You have the template in this line:

$pc=unserialize($pc1['know1']);

You'll want to do pretty much the same thing for the other three columns.



Want the best answers? Ask the best questions! TANSTAAFL!
 
wow, ok, ive managed to get hold of the origanal files and i stiil cant get it to work.


form.php
Code:
<form action="insert.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="image" id="submit" alt="Submit" src="../img/btn_submit.gif" style="border:0px;" />
</form>

insert.php
Code:
$pc1=serialize($_POST['knowledge1']);
$pc2=serialize($_POST['knowledge2']);

	
	$query="UPDATE HS21 SET knowledge_1 = '$pc1', knowledge_2 = '$pc2', submitted_date=now()   WHERE userid = $userid LIMIT 1";
	$result=mysql_query($query);

		if ($result){ 
		   echo "<h3>Submitted</h3>
      successfully submitted.<br><br>
        <a href='index.php'>Click Here</a> to continue<br><br>";
		}
		else{
	echo "<h3>ERROR</h3><p> There was a Problem submitting.";
	}
}

display.php
Code:
 <?
  $result = mysql_query("SELECT * FROM HS21 WHERE userid = '$userid'");

    while($row = mysql_fetch_array($result))
        {
        
        $pc=unserialize($pc1['knowledge_1']);
$pc=unserialize($pc2['knowledge_2']);

        
        foreach($pc as $result)
 {
   echo "$result</br>";
 }
 ?>

Im getting this error

Parse error: parse error, unexpected '[', expecting ')' in /home/n/v/nndp/public_html/ses/hs21/display.php on line 338


What am i doing wrong?

Thanx for the help

Soph
 
Insufficient data for a meaningful answer.

What line is line 338 in display.php?

Show me that line and the 10 lines preceding it.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Code:
<?
  $result = mysql_query("SELECT * FROM HS21 WHERE userid = '$userid'");

    while($row = mysql_fetch_array($result))
        {
LINE 338 ------->>>>>>>>>       
        $pc=unserialize($pc1['knowledge_1']);
$pc=unserialize($pc2['knowledge_2']);

        
        foreach($pc as $result)
 {
   echo "$result</br>";
 }
 ?>

Soph
 
Insufficient data for a meaningful answer.

If PHP reports a parse error on line 338, that doesn't necessarily mean that's where the actual error is. It just means that that's where the parser realized that the script code no longer made sense. The actual program error could be several lines or several dozens of lines earlier in the script.

Which is why I asked for line 338 and the 10 lines preceding it.



Want the best answers? Ask the best questions! TANSTAAFL!
 
sorry about that sleipnir, i should read the post more carefully.

There is a large chunk of code before 338 so ive included the lot

Code:
      <?php
$result = mysql_query("SELECT * FROM HS21 WHERE userid = '$userid'");

	while($row = mysql_fetch_object($result))
		{
			echo "<table class='main' cellpadding='0' cellspacing='0'>"; 
			echo "<tr>";
			echo "<td valign='top'>";
			echo "<div align='center'><b>HS21<br><br>
			<b><br><br>Q1.</b><br>
			<em>Why is it important?</em><br><br>";
			echo nl2br($row->answer1);
			echo "<br><br><b>Q2.</b><br>
			<em>People’s differences</em><br><br>";
			echo nl2br($row->answer2);
			echo "<br><br><b>Q3.</b><br>
			<em>Information you need</em><br><br>";
			echo nl2br($row->answer3);
			echo "<br><br><b>Q4.</b><br>
			<em>Give 3 examples?</em><br><br>";
			echo nl2br($row->answer4);
			echo "<br><br><b>Q5.</b><br>
			<em>Write below what you found out.</em><br><br>";
			echo nl2br($row->answer5);
			echo "<br><br><b>Q6.</b><br>
			<em>Why is it important?</em><br><br>";
			echo nl2br($row->answer6);
			echo "</td>";
			echo "</tr>";
			echo "</table>";
	}
?>
      <a name="knowledge" id="knowledge"></a> <br>
      <br>
      <table width="99%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td width="86%"><h2 style="font-size:14px;">Checklist</h2></td>
          <td width="14%"><a href="#top" class="img"><img src="../../nvq/img/btn_top.gif" width="100" height="19" /></a></td>
        </tr>
      </table> <?
  $result = mysql_query("SELECT * FROM HS21 WHERE userid = '$userid'");

    while($row = mysql_fetch_array($result))
        {
        
        $pc=unserialize($pc1['knowledge_1']);
		$pc=unserialize($pc2['knowledge_2']);
        
        foreach($pc as $result)
 {
   echo "$result</br>";
 }
 ?>

soph
 
I don't see anything.

I noticed that you're fetching data from MySQL into $row, but referencing $pc in this code:

while($row = mysql_fetch_array($result))
{

$pc=unserialize($pc1['knowledge_1']);
$pc=unserialize($pc2['knowledge_2']);

is that right? It shouldn't cause the parse error, though.

You're going to have to try commenting-out blocks of code to see where the error is.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Hi,

I've taken out all unessacery code and am left with this:

Code:
<?
session_start();
header("Cache-control: private");
include '../../admin/inc/xz73rlnf5ef.php';
include '../../admin/inc/s_check.inc';

if ($_SESSION['authorized']!== $sec_check){// security
header ("Location: [URL unfurl="true"]https://nv.raining.co.uk/login.php");[/URL]
exit();
} else {}
?>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?
  $result = mysql_query("SELECT * FROM HS21 WHERE userid = '$userid'");

    while($row = mysql_fetch_array($result))
        {
        
        $pc=unserialize($row['knowledge_1']);
        $pc=unserialize($row['knowledge_2']);
        
        foreach($pc as $result)
 {
   echo "$result</br>";
 }
 ?>
</body>
</html>

Im still getting a parse error on line 38

Help needed, please

Soph
 
you have not closed the curly brace for the while loop.

i personally recommend using the alernative syntax instead of curly braces. it makes debugging so much easier. if you don't like this syntax then my next recommendation is be religious about indenting.

Code:
while($row = mysql_fetch_array($result)):
        $pc=unserialize($row['knowledge_1']);
        $pc=unserialize($row['knowledge_2']);
        
        foreach($pc as $result):
           echo "$result</br>";
        endforeach;
endwhile;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top