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 multiple rows and pull arrays 1

Status
Not open for further replies.

sophielois1

Technical User
May 2, 2006
16
GB
Hi,

I hope somebody can help. Im trying to pull the serialized arrays i have stored in my table, and get the values for each array and assign them to say something like

$array1 = unserialize($row['knowledge_1']);
$array2 = unserialize($row['knowledge_2']);
$array3 = unserialize($row['knowledge_3']);
$array4 = unserialize($row['knowledge_4']);
$array5 = unserialize($row['knowledge_5']);
etc
etc


this is what i have so far.unfortunately it is not working. It is outputting 'there was a problem'.

Code:
<?php


	  $sql = "SELECT knowledge_1 knowledge_2 knowledge_3 knowledge_4 knowledge_5 knowledge_6 knowledge_7 knowledge_8 knowledge_9 knowledge_10 knowledge_11 knowledge_12 knowledge_13 knowledge_14 FROM HS31 WHERE userid = $userid LIMIT 1";
  if ($result = mysql_query($sql)) {
    $row = mysql_fetch_assoc($result);
	$array = array();
    $array = unserialize($row['knowledge_1']);
    print_r($array);
  } else {
  echo "there was a problem";
  }

?>

Can anybody help me please?

thanks
soph
 
Why don't you print out $userid, or $sql in your error catch and see if the query even looks right.
 
i suspect you need commas between each field you want to select in your query
 
Hi,

i have got it working, but at the moment only for a single row. I need to get the values for
$row['knowledge_3']
$row['knowledge_4']
$row['knowledge_5']
$row['knowledge_6']
$row['knowledge_7']
$row['knowledge_8']
up to 19




Code:
<?php


	  $sql = "SELECT * FROM HS31 WHERE userid = $userid LIMIT 1";
  if ($result = mysql_query($sql)) {
    $row = mysql_fetch_assoc($result);
    $array = array();
    $array = unserialize($row['knowledge_1']);
    print_r($array);

  } else {
  echo "there was a problem";
  }

?>

How would i do this?

thank you
Soph
 
Code:
   $array = unserialize($row['knowledge_1']);
    print_r($array);

one way would be to do this for each of 1-19.

an automated solution would be
Code:
for ($i=1; $i<20; $i++){
  print_r(unserialize($row["knowledge_$i"]));
}
 
Thank you jpadie.

Is it possible to define each array with a unique identifier e.g

arrayname1
arrayname2
arrayname3
arrayname4
arrayname5
etc

so i coud then queery the content of each array.

Soph?
 
of course
Code:
for ($i=1; $i<20; $i++){
  $array[$i]= unserialize($row["knowledge_$i"]);
}
 
mmmm,

that now returns no data.

This is the problem i have been having. For some reason when they are given unique identifiers it just won't have it.

This is the original problem that i was finding until i scrapped everything and asked you guys.

It dont figure?

This code you gave me works great
Code:
for ($i=1; $i<20; $i++){
  print_r(unserialize($row["knowledge_$i"]));
}

returning
Code:
Array ( [0] => A [1] => D ) Array ( [0] => D ) Array ( [0] => C ) Array ( [0] => C ) Array ( [0] => D ) Array ( [0] => C ) Array ( [0] => C ) Array ( [0] => C ) Array ( [0] => C ) Array ( [0] => B ) Array ( [0] => B ) Array ( [0] => B ) Array ( [0] => B ) Array ( [0] => B ) Array ( [0] => A [1] => B [2] => C ) Array ( [0] => C ) Array ( [0] => C ) Array ( [0] => C ) Array ( [0] => C )

yet

Code:
for ($i=1; $i<20; $i++){
  $array[$i]= unserialize($row["knowledge_$i"]);
}

returns absolutly nothing

Thank you for your help jpadie
 
well. it won't return anything to the screen. it's not supposed to.

if you want it on the screen then try adding
Code:
print_r($array);
 
thanks again jpadie... I'm learning thank you.

Im slightly confused...How would i queery the following to find out what values it holds

[1] => Array ( [0] => A [1] => D )

something like

if { ([1] => Array [0] == 'A'){
echo"It is A";
}

Soph
 
you're opening a hornet's nest here. it really is worth reading the manual on arrays. once you get your head around them it's a doddle.

you reference an array element by (for example)
Code:
echo $array[1][1];
which would output
Code:
D

so for your example it would be
Code:
if ($array[1][0] === "A"):
 echo "oh yeah";
else:
 echo "but no...";
endif;
 
Hi jpadie,

i'm going to go away and do some reading.

thank you for your time and help

Soph
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top